我正在开发一个 ASP.NET MVC 页面。在我的开发机器上使用 IIS Express 进行调试效果很好,起始页是
localhost:xxxx/Home/Index
。
在“真正的”IIS(不同的机器,版本 8.5)上发布后,起始页没有正确的 URL。显示
Home/Index
页面,但仅显示为http://[servername]:[port]
。路线/Home/Index
似乎没有适用。
问题:页面使用 AJAX 作为自动完成字段。当然,该操作是“未找到”,因为在 AJAX 调用中,URL 是错误的(控制器名称丢失)。
我在顶部放置了一个操作链接,结果是
/Home/Index
。我点击它,/Home/Index
正确使用和显示,自动完成按预期工作。
这是我在 IIS 配置或 ASP.NET MVC 应用程序中犯的错误吗?
我的
RouteConfig
:
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: "Index",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Global.asax
:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
更改
"Index"
路线,如下所示:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Index",
url: "Home/Index",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
并在“索引”操作方法中使用
RedirectToRoute()
:
public ActionResult Index()
{
if (HttpContext.Request.Path == "/")
{
return RedirectToRoute("Index");
}
return View();
}