asp.net-mvc - ASP.NET MVC 路由从 html 页面开始

标签 asp.net-mvc

我正在使用 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/

相关文章:

c# - 使用 IOption 的类的实例化

c# - 不能多次枚举查询的结果 (C#)( Entity Framework )(存储过程)

javascript - 如何通过mvc4中的提交按钮传递自定义参数

c# - 简单注入(inject)器 - 自定义 WebViewPage

c# - 在 View 或 Controller 之外的 ASP.NET MVC 中生成 url

asp.net - *.cshtml 中的 UTF-8

javascript - MVC 在 jQuery 中更新 HiddenFor 并提交到 Controller

asp.net-mvc - 如何遍历 FormCollection 以检查文本框是否具有值?

c# - 使用 ajax 将 DropDownList 中的值传递给 Controller

javascript - 判断jquery当前触发的是哪个事件