c# - 找不到 ASP.NET View

标签 c# asp.net asp.net-mvc asp.net-web-api razor

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

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

我不明白为什么它找不到 View ,或者为什么它甚至试图找到具有查询字符串参数名称的 View 。

最佳答案

大概能找到view。这是它找不到的母版页。

那是因为你正在使用

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.

线索是它在搜索 View 时包含了参数值。因为您传递的平台参数是一个字符串,所以它匹配调用到使用 string viewNamestring masterName 参数的方法。

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

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

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

关于c# - 找不到 ASP.NET View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38675848/

相关文章:

c# - 使用 XNodeEqualityComparer 或 XElement.DeepEquals 来比较 xml 对象更好吗?

c# - MVC4 WebApi 进程启动器

asp.net-mvc - 如何在 Visual Studio 2012 或 Visual Studio 2013 中打开旧的 MVC 项目?

asp.net-mvc - 从 Azure Function 调用 SignalR

c# - 如何在 kentico smartsearch 中按类别搜索

c# - 使用 Asp.net mvc 或 web 服务会更好吗?

c# - Interlocked.Exchange<T> 是 Microsoft 的预期 Swap 方法吗?

c# - Xamarin Forms 为 Android 带来光明

c# - 两个日期之间的 Lambda 显式

asp.net - 如何在我的 ItemTemplate 元素中维护两种不同的对齐方式,每个子元素对应一种对齐方式?