Asp.net MVC路由-使用约束阻止到XML文件的路由 [英] Asp.net MVC Routing - Preventing a route to an XML file with a constraint

查看:74
本文介绍了Asp.net MVC路由-使用约束阻止到XML文件的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来阻止用户访问特定的xml文件。我试着做...

I'm attempting to find a way to preventing a user from accessing a specific xml file. I've tried doing...

routes.MapRoute(
     "SiteMap",
     "SiteMap/siteMap.xml",
     new { },
     new { isLocal = new LocalHostRouteConstraint() });

LocalHostRouteConstraint()在哪里...

Where the LocalHostRouteConstraint() is...

public class LocalHostRouteConstraint : IRouteConstraint
{
    public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        return !httpContext.Request.IsLocal;
    }
}

这是此页面上的实现...

This is the implementation on this page...

http://www.asp.net/mvc/tutorials/creating-a-custom-route-constraint-cs

但仍然允许使用路由

还有另一种方法可以防止这种情况吗?

Is there another way to prevent this?

编辑 >
忘了忽略它!在LocalHostRouteConstrsint类中。

EDIT Forgot to mention to ignore the ! In the LocalHostRouteConstrsint class. Was doing that for testing for it to work.

推荐答案

限制是否必须在MVC路由中进行?

Does the restriction have to be in the MVC routes?

如果没有,则可以在< / configuration> 结束标记之前放置以下内容web.config

If not, you can put the following right before the </configuration> closing tag in your web.config:

<location path="SiteMap/siteMap.xml">
     <system.web>
        <authorization>
           <deny users="*"/>
        </authorization>
     </system.web>
 </location>

这篇关于Asp.net MVC路由-使用约束阻止到XML文件的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆