在 IIS 8 上设置 MVC 站点的默认路由

Setting the default route for MVC site on IIS 8

我已经使用 IIS8 在我们的网络服务器上设置了一个站点。 URL 是 link ABC.mycompany.com。当我转到 ABC.mycompany.com 时,出现未找到错误:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

如果我转到 ABC.mycompany.com/Home/InstrumentList,它会显示我想要开始的正确页面。我如何让它从这条路线开始?

在 Visual Studio 2013 年,我将 Home/InstrumentList 设置为我的开始操作并且效果很好。我查看了几个引用默认页面和索引页面的示例,但我的应用程序没有这些。此外,还有对 .aspx 页面的引用,但我的应用程序中没有 .aspx 页面,只有 .cshtml 视图和 .cs 控制器。我也看到了关于添加路由的事情,但我不确定将它们放在 global.asax.cs 中的什么位置(在 ApplicationStart 中?在它外面?以不同的方法?只是一个人?)。我也不知道要更改什么才能为我工作(将索引更改为 InstrumentListing 或保留它)。到目前为止,没有任何效果。

这是我使用 global.asax.cs 尝试添加注册路由方法的尝试之一:

namespace GPC
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            WebApiConfig.Register(GlobalConfiguration.Configuration);
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute(
            "Default",
            "{controller}/{action}/{Filtre}",
            new { controller = "Home", action = "Index", Filtre = UrlParameter.Optional });
        }
    }
}

以下是我使用过的一些网站,但对我没有任何帮助:

http://weblog.west-wind.com/posts/2013/Aug/15/IIS-Default-Documents-vs-ASPNET-MVC-Routes

http://weblogs.asp.net/imranbaloch/editing-routes-in-mvc

如果有人能告诉我如何让 ABC.mycompany.com/Home/InstrumentList 在用户转到 ABC.mycompany.com.

时显示,我将不胜感激

我不确定,但是您的 RegisterRoutes 方法应该在 RouteConfig class 中,默认情况下应该在 MVC 项目的 App_Start foulder 中。 这不是 MvcApplication class 中位于 Global.asax.cs 中的方法,正如我在您的问题中看到的那样。

那么你应该只改变 RegisterRoutes 这样的方法来设置不同的默认控制器操作:

 public static void RegisterRoutes(RouteCollection routes)
 {
     routes.MapRoute(
     "Default",
     "{controller}/{action}/{Filtre}",
     new { controller = "Home", action = "InstrumentList", Filtre = UrlParameter.Optional });
 }