.NET Core MVC默认路由与我的控制器/操作不匹配

时间:2019-05-24 19:51:46

标签: c# asp.net-core asp.net-core-mvc asp.net-core-routing

Startup.ConfigureServices

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

Startup.Configure

app.UseMvc(routes =>
{
   routes.MapRoute(
      name: "default",
      template: "{controller=Home}/{action=Index}/{id?}");
});

视图/ 首页 /Index.cshtml

@model FooViewModel
@{
    ViewData["Title"] = "Home page title";
}

<div class="row mt-2">
    @Html.DisplayForModel()
</div>

视图/ 选项 /Index.cshtml

@model BarViewModel
@{
    ViewData["Title"] = "Other page title";
}

<div class="row mt-2">
    @Html.DisplayForModel()
</div>

OptionsController

public class OptionsController : BaseController
{
    public OptionsController(){}        
    public IActionResult Index() => View(new BarViewModel());
}

OptionsController.Index()处达到断点后,我收到一条错误消息,指出:

  

InvalidOperationException:传递到ViewDataDictionary的模型项的类型为'BarViewModel',但是此ViewDataDictionary实例需要类型为'FooViewModel'的模型项。

...因为在我的FooViewModel中使用了Views/Home/Index.cshtml,所以我认为ViewEngine或其他正在查看Views / Home /Index.cshtml文件的视图而不是Views / 选项 /Index.cshtml。

有什么建议吗?我想念什么?

0 个答案:

没有答案