404具有现有路由的现有页面上的错误

时间:2015-05-26 15:35:21

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

问题

当页面和路线存在时,404 Error上的

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>

1 个答案:

答案 0 :(得分:0)

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

的Global.asax

首先,请注意AreaRegistration.RegisterAllAreas();之前放置RouteConfig.RegisterRoutes(RouteTable.Routes);的方式。路由按其接收的路由配置顺序优先。 AreaRegistration优先于RouteConfig。

RouteConfig.cs&amp; 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首先出现,这两者之后的服务路线将无效。