404在具有现有路由的现有页面上出错

本文关键字:出错 路由 | 更新日期: 2023-09-27 17:50:50

问题

当存在页面和路由时,CCD_ 1在CCD_。


问题

导致此路由问题的原因是什么?


注意观察

我不能理解的是https://localhost:44300/&https://localhost:44300/articles是唯一有效的东西。


GitHub项目

https://github.com/josephmcasey/me


感兴趣的文件

/区域/文章/文章注册Area.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 }
            );
        }
    }
}

/应用程序启动/路由配置.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);
        }
    }
}

/控制器/服务控制器.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();
        }
    }
}

/视图/服务/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 = "";
}

/视图/服务/Services.shtml

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

404在具有现有路由的现有页面上出错

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


Global.Asax

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


RouteConfig.cs&文章区域注册.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 }
        );

因此,由于Article Area Registration优先,这两条之后的服务路线将不起作用。