我正在使用 IIS 6。我认为我的问题是我不知道如何使用 paths.MapRoute 路由到非 Controller 。
我有一个 url,例如 example.com,我希望它为 index.htm 页面提供服务,而不是使用 MVC。我该如何设置?在 IIS 中,我将 index.htm 作为我的起始文档,而我的 global.asax 具有标准的“默认”路由,它在其中调用 Home/Index。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
我添加了这个:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Context.Request.FilePath == "/") Context.RewritePath("index.htm");
}
它有效。但这是最好的解决方案吗?
最佳答案
我添加了一个虚拟 Controller ,在指定网站的根目录时用作默认 Controller 。该 Controller 有一个索引操作,可以重定向到根目录下的index.htm 站点。
public class DocumentationController : Controller
{
public ActionResult Index()
{
return Redirect( Url.Content( "~/index.htm" ) );
}
}
请注意,我使用的是基于 MVC 的 REST Web 服务的文档。如果您转到站点的根目录,您将获得该服务的文档,而不是某些默认的 Web 服务方法。
关于asp.net-mvc - ASP.NET MVC 路由从 html 页面开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/794753/