【发布时间】:2016-08-06 17:56:09
【问题描述】:
它必须是我缺少的一些简单的东西,因为这是我第一次尝试 MVC。似乎除了默认路线之外没有任何作用。
这是我的步骤:
- 从 MVC5 模板创建新的解决方案(它带有 HomeController 和一个 Controller/Action/Id 默认在 路由器配置。
- 添加 --> 名为 Foo 的控制器 --> 创建 一个名为 Details 的 ActionResult
- 添加一个名为Details的视图
- 运行 MVC 项目
- 出现首页,我改了网址
本地主机:50212
到
localhost:50212/Foo/详细信息
结果:我得到了 404
MVC AreaRegistration 不会在编译时自动发生吗?
我去了 Global.asax 并尝试放置在 Application_Start
AreaRegistration.RegisterAllAreas();
但这似乎没用。有任何想法吗? VS community 2015 是否缺少某些东西? 问候
控制器/FooController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcTest.Controllers
{
public class FooController : Controller
{
// GET: Foo
public ActionResult Details()
{
return View();
}
}
}
Views/Foo/Details.cshtml
@model MyModel
Hello
Global.asax 的 App_start
namespace MvcTest
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
AreaRegistration.RegisterAllAreas();
//This up here is something I added manually after its byDefault scaffolding
FilterConfig.Configure(GlobalFilters.Filters);
RouteConfig.Configure(RouteTable.Routes);
}
@Edit,添加 FooController 和 Views/Foo/Details
@@Edit:将缺失的 s 添加到 Details。同样的错误。
@@@Edit:共享 Global.asax
【问题讨论】:
-
能把
Foo控制器的代码发一下吗? -
行,给我 5'
-
基于此代码,您不会将模型传递给您的视图。那应该给出一个错误。如果您的代码是这种情况,请尝试删除
@model MyModel行,看看会发生什么。 -
模型错误会是别的东西,对吧?不是 404。
-
如果你必须自己生成这些,肯定有问题。默认的 MVC 应用程序应该为您生成所有这些。我已经为你准备了一个可行的解决方案。我所能建议的就是看看它以找出差异。你可以在这里找到它:dropbox.com/s/d4t1rfuu5o0dq56/SO-38807208.zip?dl=0
标签: asp.net-mvc asp.net-mvc-5 vs-community-edition mvcroutehandler