首先选择路由,然后偏向静态文件



这是我的RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {     
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

这是我的控制器:

[RoutePrefix("dark")]
public class DarkController : Controller
{
    public DarkController()
    {
        this.ViewBag.LayoutName = "_DarkLayout.cshtml";
    }
    [Route("index.html")]
    public ActionResult Index()
    {
        return View();
    }
}

在这里,如果我访问/Dark/index.html,它将加载Dark文件夹中存在的物理index.html文件,但是如果我从深色文件夹中删除index.html文件,我的路线似乎可以正常工作。

我只想给出我的路线的1ST优先级,如果找不到这一点,那么它应该转到物理文件。这里似乎正在发生相反的情况,并且对物理文件提供了第一个偏好,如果找不到那是我的路线。

我也在我的web.config中有一个:

<modules runAllManagedModulesForAllRequests="true">
    <remove name="ApplicationInsightsWebTracking"/>
    <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
        preCondition="managedHandler"/>
</modules>

有什么方法来解决此问题?

您可以为*.html文件编写自己的HttpHandler,并在处理程序内部路由。

例如:

public class HtmlFileHandler: IHttpHandler
{        
    public void ProcessRequest(HttpContext context)
    {
        var htmlFileRequested = HttpContext.Current.Request.Url.Segments.Contains(".html");
        if (htmlFileRequested)
        {
          // return file by context.Response.WriteFile("");
          // don't forget: context.Current.ApplicationInstance.CompleteRequest();
        }
        else
        {
          //redirect to controller factory
        }
    }
    public bool IsReusable
    {
        // To enable pooling, return true here.
        // This keeps the handler in memory.
        get { return false; }
    }
}

web.config:

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="*.html" 
         type="HtmlFileHandler" />
    </httpHandlers>
  </system.web>
</configuration>

有关自定义httphandlers的更多信息

最新更新