0

我正在使用 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();
        }
    }
}

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

4

1 回答 1

0

我认为您的路线定义顺序不正确,路线顺序从上到下评估(首先解决最具体的路径)。

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

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访问特定路由时处理的路由。

于 2017-08-29T01:21:00.910 回答