C# 首先选择路由,然后选择静态文件
这是我的C# 首先选择路由,然后选择静态文件,c#,asp.net,asp.net-mvc,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,这是我的RouteConfig.cs public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); routes.MapRoute(
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文件,我的路线似乎工作正常
我只想给我的路线第一优先权,如果没有找到,那么它应该去物理文件。这里似乎发生了相反的情况,第一个优先考虑的是物理文件,如果找不到,那么我的路由就会被命中
我的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>