如何在MVC中更改访问控制器优先级? [英] How to change access controller priority in MVC ?

查看:97
本文介绍了如何在MVC中更改访问控制器优先级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目 FilesController 中包含带有一个字符串参数的 MyFiles 操作方法。并且在同一个项目中还有一个名为 Files 的文件夹,其中包含 MyFiles 子文件夹。当用户请求 www.sitename / Files / MyFiles / Pic.jpg 时,它直接从文件系统访问文件而不是点击控制器操作。如何将此请求优先级表单文件级别更改为控制器级别





控制器

In my project "FilesController" contain "MyFiles" action method with one string argument. And also in the same project there is a folder named "Files" that contain "MyFiles" sub folder. When a user requesting "www.sitename/Files/MyFiles/Pic.jpg" it directly accessing the file from file system instead of hitting the controller action. How to change this request priority form file level to controller level


Controller

public class FilesController : Controller
    {      
        public string MyFiles(string fileName)
        {
            return "abc";            
        }
    }





注意: - 我们没有使用角色授权



Note :- We are not using Role authorization

推荐答案

当你运行一个MVC站点时,你有一个RouteCollection对象负责将你的Controller / Action转换为实际的调用...默认情况下,物理路径优先于Controller / Action,但是你可以通过设置RouteCollection来改变它。 RouteExistingFiles [ ^ ]在您初始化路线时为true ...

但是!如前物理路径隐藏控制器/动作现在控制器/动作将隐藏物理路径!所以如果不是你的意思,最好重命名其中一个......
When you running an MVC site you have somewhere a RouteCollection object that responsible to translate your Controller/Action like URLs to actual calls...By default physical path has a precedence over Controller/Action, but you can change that by setting RouteCollection.RouteExistingFiles[^] to true at the point you initialize your routes...
But! As before physical path hide the Controller/Action now Controller/Action will hide physical path! So if that not what you meant to do it's better to rename one of them...


这篇关于如何在MVC中更改访问控制器优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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