问题描述
我正在使用ASP.Net MVC,我想知道如何限制对特定文件的访问.我读过有关使用[授权]的信息,但它适用于控制器和动作.我尝试在web.config中使用它,但极力建议不要这样做,因为当看到多个时,MVC会变得混乱.
I am using ASP.Net MVC, and I was wondering how can I restrict access to a certain file. I read about using [Authorize] but it applies to controllers and actions. I try to use within the web.config but its extremely advise not to do that cause MVC becomes confuse when seeing multiple .
我的文件位于应用程序的库中,例如/MyApp/MyFile.我想做的是,当用户导航到文件时.用户会看到,如果我输入错误,HTTP 401错误或其他内容,请纠正我.
My file is within the base of the application, e.g. /MyApp/MyFile. I would like to do is, when a user navigates to the file. The user will see, correct me if I'm wrong, the HTTP 401 error or something.
推荐答案
为此,将文件粘贴在App_Data下的文件夹中,这将阻止直接访问,然后您需要创建一个控制器来处理对该文件的访问.首先创建一条路线:
In order to do this stick the files in a folder under App_Data which will prevent direct access and then you will need to create a controller to handle access to the file. First create a route:
routes.MapRoute("", "Files/{file}", new { controller = "File", action = "View" });
然后将Authorize属性应用于操作并使用
and then apply the Authorize attribute to the action and use
return File(...);
将文件实际返回给用户.
to actually return the file to the user.
这篇关于如何限制对MVC中特定文件的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!