2017-08-28 72 views
0

我正在MVC 5中工作,并接管了一个项目。当我登录到网页“mydomain.com”是一个错误出现:“服务器错误‘/’应用程序的资源无法找到说明:HTTP 404请求的URL:/”MVC主页不工作,RouteConfig和全局文件看起来OK

如果我输入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的原因?如果我将Default映射放置在RouteConfig文件的底部,它将自动指向登录页面。再次,我并不真正理解为什么这样做会这样。

+1

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

回答

0

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

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

​​3210

此外,在样品中的每一个路径中定义的所有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处理的路由。

+0

我现在注释了所有其他路线,并且按计划运行。现在试图找出为什么添加其他路线。谢谢。 – Zach