【问题标题】:New MVC 4 Project. Default routes are getting ignored新的 MVC 4 项目。默认路由被忽略
【发布时间】:2013-11-04 12:44:45
【问题描述】:

我刚刚创建了一个新的 MVC 4 项目,并使用 Database First 添加了一个 EDO.NET 实体数据模型。我不确定究竟是为什么,但事情似乎不像以前那样正常运作。我不得不手动添加 EF Code Generation 项来生成实体类。

无论如何,我遇到的主要问题是默认路由似乎被忽略了。 我的路由配置是默认的,如下:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

但是 [LOCALHOST]/Properties/ 没有找到 /Properties/Index,它只是在“/”应用程序中返回 404 服务器错误。 找不到资源。

我不会因为犯了一些愚蠢的错误或忘记了一些重要的事情而忘记了自己,但我已经在 StackOverflow 和互联网上搜索了类似的问题,但没有任何解决方案有任何帮助。如果有人知道原因,我将不胜感激。

请求编辑:

我有 3 个控制器:

  1. 首页 - 原封不动
  2. 帐户 - 未更改
  3. 属性 - 带默认 MVC CRUD 操作(索引、详细信息、创建、编辑)

它在 IIS 上运行良好,但在 VS 的内部调试 IIS 上运行良好。

@Html.ActionLink("Properties", "Index", "Properties") 运行时生成 http://[localhost]:53909/Properties。但是,单击生成的链接会给我一个“'/'应用程序中的服务器错误。 找不到资源。”

PropertiesController.cs(仅索引操作)

public class PropertiesController : Controller
{
    private PropertyInfoEntities db = new PropertyInfoEntities();

    //
    // GET: /Properties/

    public ActionResult Index()
    {
        //Mapper.CreateMap<Property, PropertiesListViewModel>()
            //.ForMember(vm => vm.MainImageURL, m => m.MapFrom(u => (u.MainImageURL != null) ? u.MainImageURL : "User" + u.ID.ToString()))
        //    ;
        //List<PropertiesListViewModel> properties =
        //    Mapper.Map<List<Property>, List<PropertiesListViewModel>>(db.Properties.ToList());
        return View(db.Properties.Include(p => p.Currency).Include(p => p.Type).Include(p => p.Province).Include(p => p.Region).Include(p => p.SaleType).Include(p => p.Source).Include(p => p.Town).ToList());
    }
}

_Layout.cshtml

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title - My ASP.NET MVC Application</title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <header>
        <div class="content-wrapper">
            <div class="float-left">
                <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
            </div>
            <div class="float-right">
                <section id="login">
                    @Html.Partial("_LoginPartial")
                </section>
                <nav>
                    <ul id="menu">
                        <li>@Html.ActionLink("Home", "Index", "Home")</li>
                        <li>@Html.ActionLink("About", "About", "Home")</li>
                        <li>@Html.ActionLink("Properties", "Index", "Properties")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>
....

编辑 2: 即使有特定的路线,它仍然会被忽略

namespace ProjectName
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Properties",
                "Properties/{action}/{id}",
                new { controller = "Properties", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

干杯!

【问题讨论】:

  • 你有 PropertiesController 控制器,里面有 Index 动作和 Views/Properties/Index.cshtml 视图吗?
  • @jim 是的,我拥有所有这些,并且在托管在本地 IIS 上时可以正常工作,而不是在 Visual Studio 内部/调试 IIS 上。
  • 确保您的 URL 中的端口号与您当前使用的内部 IIS 相同。
  • 您介意编辑您的问题以包含相关的控制器和视图,以便我们看看吗?
  • @JohnH 看起来像开发 IIS 服务器设置或 URL 错误

标签: c# entity-framework asp.net-mvc-4 asp.net-mvc-routing url-routing


【解决方案1】:

我尝试将 PropertiesController 的名称更改为 Properties1Controller,并且路由完全可以正常工作。经过进一步挖掘,我发现这是因为Properties is a Windows Reserved Keyword

总之,解决问题的方法:不要使用保留关键字作为控制器名称。

奇怪的是,/Properties/Index 的路由完全正常,而 /Properties/ 的路由在生产 IIS 上完全正常,只是不适用于开发。这使得解决问题变得更加困难,但最终还是设法解决了问题。

感谢大家的帮助。

【讨论】:

    【解决方案2】:

    在您的 Global.asax.cs 文件中,您是否注册了路线?例如:

    RouteConfig.RegisterRoutes(RouteTable.Routes);
    

    【讨论】:

    • +1:实际上,我认为这是对的。如果视图丢失,它会给出错误,列出搜索的位置。
    • 是的,RouteConfig.RegisterRoutes(RouteTable.Routes) 是根 Global.asax 中的第 23 行。
    【解决方案3】:

    访问localhost/properties 明确表示您要调用PropertiesController。如果您的意思是希望它成为您的默认路由,那么您需要将您的路由配置更改为以下内容:

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Properties", action = "Index", id = UrlParameter.Optional }
    );
    

    这将允许您直接从localhost 调用它。但是,听起来好像您缺少 .cshtml 文件。您确定~/Views/Properties/Index.cshtml 存在吗?

    【讨论】:

    • 不,我希望 Home 成为我的默认控制器。属性是另一个控制器。我有一个生成 [Localhost]/Properties/ 的 @Url.Action("Properties", "Index")。但是当我单击该链接时,它找不到该页面,因为它忽略了索引操作的路由。
    • @MatthewHudson 浏览器中该 href 上显示的确切链接是什么?
    • @MatthewHudson 我想不出其他的了,但你有没有不小心用[HttpPost] 或类似的东西装饰了你的PropertiesController.Index 动作?
    • @jim http://[localhost]:53909/Properties 是它创建的链接,它返回 404 服务器错误。但是,如果我手动添加 /Index 则它完全可以正常工作。
    • @JohnH 不,这是一个空项目。我添加的只是一个带有默认 MVC 操作的 EDMX 和属性控制器。
    【解决方案4】:

    默认路由意味着每个 [localhost]/foo/bar 请求都被转发到 FooController 及其必须返回一些现有视图的 Bar 操作。可能您没有其中一些。
    “defaults”参数设置默认控制器和动作名称,以防它们未在请求中指定(即 http://[localhost]/),因此在您的情况下,这将搜索 HomeController 及其索引动作。

    【讨论】:

    • 我的理解是 [localhost]/Properties 应该路由到 Properties 控制器的 Index 操作。但是,除非我手动将 /Index 添加到 URL,否则我会收到 404 服务器错误
    【解决方案5】:

    Properties 控制器的命名空间是否符合“ProjectNamespace.Controllers”约定?如果您从其他来源复制代码,您可能忘记更改控制器类的命名空间。

    【讨论】:

    • 没错,就是:命名空间ProjectName.Controllers
    猜你喜欢
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多