ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

找不到ASP.NET视图

2019-10-26 21:06:13  阅读:114  来源: 互联网

标签:razor asp-net-web-api c asp-net-mvc


我在Visual Studio中创建了一个Web API项目.我正在使用属性路由.这是Controllers文件夹下的控制器:

public class RegistrationController : Controller
{
    // GET: Registration
    [Route("")]
    public ActionResult CreateUser(string platform)
    {

         return View("~/Views/Registration/CreateUser.cshtml", platform);
    }
}

当我通过URL http:// localhost / application调用CreateUser操作时,它可以工作,但是当我尝试通过URL http:// localhost / application?platform = android传递查询字符串参数时,会出现以下错误:

The view ‘~/Views/Registration/CreateUser.cshtml’ or its master was not found or no view engine supports the searched locations. The
following locations were searched:

~/Views/Registration/CreateUser.cshtml

~/Views/Registration/android.master

~/Views/Shared/android.master

~/Views/Registration/android.cshtml

~/Views/Registration/android.vbhtml

~/Views/Shared/android.cshtml

~/Views/Shared/android.vbhtml

我不明白为什么它在那里时找不到视图,或者为什么它甚至试图用查询字符串参数的名称来找到视图.

解决方法:

它可能可以找到视图.它是找不到的母版页.

那是因为您正在使用

class Controller : ... {
    ViewResult View(string viewName, string masterName);
}

重载方法

Creates a System.Web.Mvc.ViewResult object using the view name and
master-page name that renders a view to the response.

提示是在搜索视图中包含参数值.因为您传递的platform参数是一个字符串,所以它与使用字符串viewName和string masterName参数的方法相匹配.

控制器的ViewResult View()方法有很多过载.在这种情况下,您可能希望将平台作为对象模型传递.您可以通过使用命名参数来解决此问题,这可以通过让编译器知道要调用的重载方法来避免混淆.

public class RegistrationController : Controller {
    // GET: Registration
    [Route("")]
    public ActionResult CreateUser(string platform) {
         return View("~/Views/Registration/CreateUser.cshtml", model: platform);
    }
}

从那里一切都应该按预期工作

标签:razor,asp-net-web-api,c,asp-net-mvc
来源: https://codeday.me/bug/20191026/1939441.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有