[英]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.