Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NETMVC-文件访问_C#_Asp.net_Asp.net Mvc_File_Routing - Fatal编程技术网

C# ASP.NETMVC-文件访问

C# ASP.NETMVC-文件访问,c#,asp.net,asp.net-mvc,file,routing,C#,Asp.net,Asp.net Mvc,File,Routing,我试图在我的应用程序中以这样的方式配置路由:文件路径应该适用于除一个特殊目录之外的每个目录。对该目录中文件的访问应由我的控制器操作处理。如果我这样写smth: routes.Map("Specialfolder/{name}", "Controller", "Action"); 它仅适用于不存在的文件。控制器不捕获现有文件的路由 如果我添加这一行: routes.RouteExistingFiles = true; 使用“我的文件夹”中的文件是可以的。但其他目录中的文件不再路由。如何修复此问

我试图在我的应用程序中以这样的方式配置路由:文件路径应该适用于除一个特殊目录之外的每个目录。对该目录中文件的访问应由我的控制器操作处理。如果我这样写smth:

routes.Map("Specialfolder/{name}", "Controller", "Action");
它仅适用于不存在的文件。控制器不捕获现有文件的路由

如果我添加这一行:

routes.RouteExistingFiles = true;

使用“我的文件夹”中的文件是可以的。但其他目录中的文件不再路由。如何修复此问题?

下面是一个示例ActionFilter示例代码片段,您可以使用它

public class UriActionFilter : System.Web.Mvc.ActionFilterAttribute
{
    public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
    {
        if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
        {
            // Sample: somehow identify the user, in case of a custom identity, replace the below line to get the user identifier
            var user = System.Threading.Thread.CurrentPrincipal.Identity.Name;

            // get the parameter that will let you know what is the image or path that is being requested now
            var ctxParams = filterContext.ActionParameters;

            // if the user does not have the permission to view, return 403
            filterContext.RequestContext.HttpContext.Response.StatusCode = 403;
            return;
        }
        base.OnActionExecuting(filterContext);
    }
}
在这个代码段中,您必须替换为相应的服务调用等

W.R.
OnAuthorization
,我提到您可以使用,以便任何访问图像的人都可以直接命中URI,并且可以使用它进行过滤。我们使用这种方式来限制给定用户直接访问URI


这将帮助您覆盖控制器获得的每个请求的MVC授权。这更像是URI本身的限制。但是,上述操作筛选器也可以满足您的情况,因为uri参数的解析有时会非常棘手。

您是否尝试将文件扩展名用作路由配置的一部分。你可以tryhttp://haacked.com/archive/2008/03/13/url-routing-debugger.aspx/You 可以尝试使用操作筛选器来允许/不允许对该特定控制器的请求。因此,基于您在该操作筛选器中指定的条件,响应将被服务或拒绝。@saravanan感谢链接,我可以将其用于测试。但您的意思是要在routes中写入所有现有的文件扩展名吗?@Venkat以及如何路由此操作?当然,我的意思是,我可以创建操作,它将返回文件(…)。但问题是如何写RouteEnees,希望你没有太多,或者只是做一个倒转的条件,这会很容易,你能举个例子用RouteEnfig写什么吗?我应该启用“RouteExistingFiles”吗?之后如何使用过滤器?类似这样的
routes.IgnoreRoute({*staticfile}),new{staticfile=@.*\(ico | css | js | gif | jpg | woff | eot | svg | eot | ttf | otf)(/.*))我使用它跳过静态文件的路由。您也可以形成一个类似的正则表达式以适合您的用例谢谢,我终于实现了它!我使用了这个:
routes.IgnoreRoute({*staticfile}),new{staticfile=@”(\w| \-\124; \.)*\(png | ico | css | js | gif | jpg | woff | eot | svg | eot | ttf | otf | | | | | | | | | |
routes.IgnoreRoute({*staticfile}),新的{staticfile=@“内容/(\w | \-\124;/)*\(png | ico | css | js | gif | jpg | woff | eot | svg | eot | ttf | otf | | | | | | | | | |。对于被阻止的文件,我使用了
routes.Map(“Myfolder/{name}”、“General”、“GetBlockedData”)我真的没有使用动作过滤器,因为我的问题只是你评论中的“routes.IgnoreRoute:)