[英]First preference to routing then to static file
这是我的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
文件,但是如果我从Dark
文件夹中删除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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.