MVC主页不工作,RouteConfig和全局文件看起来还可以



我在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文件的底部,它会自动指向登录页面。再说一遍,我真的不明白为什么会这样。

我认为您的路由定义顺序不正确,路由顺序从上到下计算(首先解析最具体的路径(。

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

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

相关内容

  • 没有找到相关文章

最新更新