【问题标题】:MVC homepage not working, RouteConfig and Global files look OKMVC 主页不工作,RouteConfig 和全局文件看起来不错
【发布时间】:2018-02-06 02:42:38
【问题描述】:

我正在使用 MVC 5 并接手了一个项目。当我登录主页“mydomain.com”时出现错误:

“'/'应用程序中的服务器错误。找不到资源。描述:HTTP 404。请求的 URL:/”

如果我输入 mydomain.com/home/index,它会显示应有的主页。我认为这是一个 RouteConfig.cs 问题,但对我来说一切看起来都很默认。

namespace Source
{
    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 }
            );

            routes.MapRoute(
                name: "Glossary2",
                url: "{controller}/{action}/{letter}",
                defaults: new { controller = "Teacher", action = "Glossary2", letter = UrlParameter.Optional }
                );

            /* Will need to finish this later.
             * When the contact us form is submitted, it should redirect to Home/Contact/{state}
             * where {state} can be either 'Success' or 'Error', which will display
             * an alert component in the view based on the provided {state}.
             */
            routes.MapRoute(
                name: "ContactSuccess",
                url: "{controller}/{action}/{submissionStatus}",
                defaults: new { controller = "Home", action = "Contact", submissionStatus = UrlParameter.Optional }
                );

            /* Will need to finish this later.
             * When the contact us form is displayed, it should check to see if a reason for contacting
             * us is already set. If it is, it should automatically select the appropriate reason on the
             * dropdown menu.
             */
            routes.MapRoute(
                name: "ContactReason",
                url: "{controller}/{action}/{reason}",
                defaults: new { controller = "Home", action = "Contact", reason = UrlParameter.Optional }
                );
        }
    }
}

我不确定 CustomeViewEngine 在做什么,也没有真正搞砸它。我还检查了 Global.asax.cs 文件,它看起来也很标准。

namespace Source
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            ViewEngines.Engines.Add(new CustomViewEngine());
        }
    }
    public class CustomViewEngine : RazorViewEngine
    {
        public static readonly string[] CUSTOM_PARTIAL_VIEW_FORMATS = new[]
            { "~/Views/Selection/{0}.cshtml" };
        public CustomViewEngine()
        {
            base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(CUSTOM_PARTIAL_VIEW_FORMATS).ToArray();
        }
    }
}

有没有办法追查为什么域名没有被路由到 home/index?如果我将默认映射放在 RouteConfig 文件的底部,它希望自动定向到登录页面。再说一次,我真的不明白为什么会这样。

【问题讨论】:

  • 所有这些路由都相互冲突。它们都有相同的路由模板。您需要更多地区分这些路线

标签: asp.net-mvc routes web-config routeconfig


【解决方案1】:

我认为您的路线定义顺序不正确,路线顺序从上到下评估 (the most specific path resolved first)。

因此,默认路由应该作为最后定义的路由:

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

    // custom path at the top (or top-most depending on priority)
    routes.MapRoute(
        name: "Example",
        url: "Example/{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    // default path at the bottom-most
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

此外,示例中每个路由中定义的所有 URL 都采用相同的模式(使用 {controller}/{action}/{parameter}),因此它们之间可能存在冲突。您可以使用纯字符串来区分相似的路由模式:

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

    routes.MapRoute(
        name: "Glossary2",
        url: "Teacher/{action}/{letter}",
        defaults: new { controller = "Teacher", action = "Glossary2", letter = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "ContactSuccess",
        url: "{controller}/{action}/SubmissionStatus/{submissionStatus}",
        defaults: new { controller = "Home", action = "Contact", submissionStatus = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "ContactReason",
        url: "{controller}/{action}/Reason/{reason}",
        defaults: new { controller = "Home", action = "Contact", reason = UrlParameter.Optional }
    );

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

注意:在访问特定路由时,使用RouteDebugger找出RouteConfig处理的路由。

【讨论】:

  • 我暂时注释掉了所有其他路线,它正在按计划工作。现在尝试找出为什么添加了其他路线。谢谢。
猜你喜欢
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多