Routes.AppendTrailingFlash排除了一些路线
本文关键字:些路 AppendTrailingFlash 排除 Routes | 更新日期: 2023-09-27 18:22:01
在MVC 5.2.2中,我可以将Routes.AppendTrailingSlash
设置为true,以便将尾部斜杠附加到URL。
然而,我也有一个机器人控制器,它返回robots.txt的内容。
如何防止Slash被附加到robots.txt路由,并使其在不带尾部斜杠的情况下可调用?
我的控制器代码:
[Route("robots.txt")]
public async Task<ActionResult> Robots()
{
string robots = getRobotsContent();
return Content(robots, "text/plain");
}
我的路线配置如下:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
RouteTable.Routes.AppendTrailingSlash = true;
操作过滤器怎么样。我写得很快,不是为了效率。我已经在手动放置和引导"/"的URL上对其进行了测试,并像符咒一样工作。
public class NoSlash : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
var originalUrl = filterContext.HttpContext.Request.Url.ToString();
var newUrl = originalUrl.TrimEnd('/');
if (originalUrl.Length != newUrl.Length)
filterContext.HttpContext.Response.Redirect(newUrl);
}
}
尝试以这种方式使用
[NoSlash]
[Route("robots.txt")]
public async Task<ActionResult> Robots()
{
string robots = getRobotsContent();
return Content(robots, "text/plain");
}
如果导航到静态.txt文件,ASP.NET的行为是在URL后面有斜杠的情况下返回404 Not Found。
我采用了@Dave Alperovich的方法(谢谢!),并返回了HttpNotFoundResult,而不是重定向到没有斜杠的URL。我认为这两种方法都是完全有效的。
/// <summary>
/// Requires that a HTTP request does not contain a trailing slash. If it does, return a 404 Not Found.
/// This is useful if you are dynamically generating something which acts like it's a file on the web server.
/// E.g. /Robots.txt/ should not have a trailing slash and should be /Robots.txt.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class NoTrailingSlashAttribute : FilterAttribute, IAuthorizationFilter
{
/// <summary>
/// Determines whether a request contains a trailing slash and, if it does, calls the <see cref="HandleTrailingSlashRequest"/> method.
/// </summary>
/// <param name="filterContext">An object that encapsulates information that is required in order to use the <see cref="System.Web.Mvc.RequireHttpsAttribute"/> attribute.</param>
/// <exception cref="System.ArgumentNullException">The filterContext parameter is null.</exception>
public virtual void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
string url = filterContext.HttpContext.Request.Url.ToString();
if (url[url.Length - 1] == '/')
{
this.HandleTrailingSlashRequest(filterContext);
}
}
/// <summary>
/// Handles HTTP requests that have a trailing slash but are not meant to.
/// </summary>
/// <param name="filterContext">An object that encapsulates information that is required in order to use the <see cref="System.Web.Mvc.RequireHttpsAttribute"/> attribute.</param>
protected virtual void HandleTrailingSlashRequest(AuthorizationContext filterContext)
{
filterContext.Result = new HttpNotFoundResult();
}
}