【问题标题】:404 Error on existing pages with existing routes具有现有路由的现有页面上的 404 错误
【发布时间】:2015-08-08 09:19:00
【问题描述】:

问题

当页面和路由存在时,404 Error on https://localhost:44300/services


问题

是什么导致了这个路由问题?


注意观察

我无法理解的是 https://localhost:44300/https://localhost:44300/articles 之类的目录是如何工作的。


GitHub 项目

https://github.com/josephmcasey/me


感兴趣的文件

/Areas/Article/ArticleRegistrationArea.cs

using System.Web.Mvc;

namespace JosephMCasey.Areas.Article
{
    public class ArticleAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "Article";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "Article",
                "{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

/App_Start/RouteConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

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

            // access TermsController/Index via /terms
            routes.MapRoute(
                name: "Terms",
                url: "terms",
                defaults: new { controller = "Terms", action = "Index" }
            );

            // access PrivacyController/Index via /privacy
            routes.MapRoute(
                name: "Privacy",
                url: "privacy",
                defaults: new { controller = "Privacy", action = "Index" }
            );

            // access ServicesController/Index via /services
            routes.MapRoute(
                name: "Services",
                url: "services",
                defaults: new { controller = "Services", action = "Index" }
            );

            // access ErrorController/NotFound via /404
            routes.MapRoute(
                name: "NotFound",
                url: "404",
                defaults: new { controller = "Error", action = "Index" }
            );

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

/Global.asax

using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace JosephMCasey
{
    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

/Controllers/ServicesController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace JosephMCasey.Controllers
{
    //[Authorize]
    [AllowAnonymous]
    public class ServicesController : Controller
    {
        [AllowAnonymous]
        public ActionResult Index()
        {
            return View();
        }
    }
}

/Views/Services/index.cshtml

@section SPAViews {
    @Html.Partial("_Services")
}
@section Scripts{
    @*
        @Scripts.Render("~/bundles/knockout")
        @Scripts.Render("~/bundles/app")
    *@
}
@{
    ViewBag.Title = "Privacy";
    ViewBag.Keywords = "";
    ViewBag.Description = "";
    ViewBag.Created = "";
    ViewBag.Thumbnail = "";
    ViewBag.Image = "";
    ViewBag.TwitterCard = "";
    ViewBag.Site = "";
    ViewBag.URL = "";
    ViewBag.ImageHeight = "";
    ViewBag.ImageWidth = "";
    ViewBag.Site = "";
    ViewBag.Creator = "";
    ViewBag.OGType = "";
}

/Views/Services/_Services.cshtml

<section class="bg-lake light row shelve">
</section>

【问题讨论】:

  • 您是否尝试过使用 RouteDebugger(nuget 包)来查看您期望被命中的路由是否是路由实际命中的路由?
  • 我没有。我什至不知道这件事。我一直在使用 CodeMaid 和 Web Essentials。我一定会检查一下,看看它是否带来了什么。我最大的问题是在没有错误消息的情况下修复某些内容。
  • 谢谢@KarenB!因为你,我得到了答案!

标签: c# asp.net asp.net-mvc routes http-status-code-404


【解决方案1】:

如果您使用的是默认的 ASP.Net MVC 5 项目,那么这可能对您很有帮助。首先,RouteDebugger Nuget 包在上次更新 3 年后仍然是一个很棒的工具。它很容易指出我的错误。


Global.Asax

首先,注意AreaRegistration.RegisterAllAreas(); 是如何放在RouteConfig.RegisterRoutes(RouteTable.Routes); 之前的。路由按照接收到的路由配置的顺序优先。 AreaRegistration 优先于 RouteConfig。


RouteConfig.cs 和 ArticleAreaRegistration.cs

接下来,Article and Portfolio areas 使用的路由配置与

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

因此,由于 ArticleAreaRegistration 在前,因此这两个之后的服务路由将不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-29
    • 2015-03-27
    • 2018-01-19
    • 1970-01-01
    • 2019-10-16
    • 2022-12-09
    • 2023-01-26
    • 2014-07-12
    相关资源
    最近更新 更多