如何在ASP中有同名的文件夹和控制器。净MVC吗?

时间:2021-09-07 16:46:54

I have a MVC controller called Downloads. http://mysite/Downloads

我有一个MVC控制器叫做下载。http://mysite/Downloads

I also want to put a physical file in a physical folder called http://mysite/Downloads/MyFile.zip.

我还想将一个物理文件放在一个名为http://mysite/Downloads/MyFile.zip的物理文件夹中。

If I simply create a folder, I get a 403 when browsing to http://mysite/Downloads. (Most likely because of directory browsing is disabled) But I want the MVC controller to kick in instead.

如果我只是创建一个文件夹,当我浏览到http://mysite/Downloads时就会得到一个403。(很可能因为目录浏览被禁用)但是我想让MVC控制器介入。

How do I do that?

我该怎么做呢?

2 个解决方案

#1


5  

If you browse to http://mysite/Downloads/{ACTION} it will fire your controllers action.

如果您浏览到http://mysite/Downloads/{动作},它将触发您的控制器动作。

The only thing that won't work in your example is the /Downloads with no action. You could re-write this URL to redirect you to your default action.

在您的示例中,惟一不能工作的是没有操作的/下载。您可以重新编写这个URL来重定向到您的默认操作。

In addition, you will need to have the routehandler ignore your download files. You can add a line in your global.asax file to ignore all zip files or some other ignore pattern that suits.

此外,还需要让routehandler忽略下载文件。您可以在全局变量中添加一行。asax文件可以忽略所有zip文件或其他适合的忽略模式。

routes.Ignore("{resource}.zip");

#2


4  

Since .NET 3.5, you can route existing files:

由于。net 3.5,您可以路由现有的文件:

public static void RegisterRoutes(RouteCollection routes) {
    routes.RouteExistingFiles = true;
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
       name: "Default",
       url: "{controller}/{action}/{id}",
       defaults: new { controller = "Home", 
                          action = "Index", 
                          id = UrlParameter.Optional }
    );
}

So suppose we had a folder on the site root called Markets containing an audio.mp3 file:

假设我们在网站根目录上有一个名为Markets的文件夹,其中包含音频。mp3文件:

\Markets
\Markets\audio.mp3

Assuming the existence of a MarketsController, if we made a request for Markets, it'd be routed to Markets/Index.

假设存在一个MarketsController,如果我们请求市场,它将被路由到Markets/Index。

If we requested /Markets/audio.mp3 we'd get the mp3 file and if we requested Markets/AnythingElse, normal routing would apply.

如果我们要求/市场/音频。mp3我们会得到mp3文件,如果我们要求市场/其他任何东西,正常的路由将适用。

#1


5  

If you browse to http://mysite/Downloads/{ACTION} it will fire your controllers action.

如果您浏览到http://mysite/Downloads/{动作},它将触发您的控制器动作。

The only thing that won't work in your example is the /Downloads with no action. You could re-write this URL to redirect you to your default action.

在您的示例中,惟一不能工作的是没有操作的/下载。您可以重新编写这个URL来重定向到您的默认操作。

In addition, you will need to have the routehandler ignore your download files. You can add a line in your global.asax file to ignore all zip files or some other ignore pattern that suits.

此外,还需要让routehandler忽略下载文件。您可以在全局变量中添加一行。asax文件可以忽略所有zip文件或其他适合的忽略模式。

routes.Ignore("{resource}.zip");

#2


4  

Since .NET 3.5, you can route existing files:

由于。net 3.5,您可以路由现有的文件:

public static void RegisterRoutes(RouteCollection routes) {
    routes.RouteExistingFiles = true;
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
       name: "Default",
       url: "{controller}/{action}/{id}",
       defaults: new { controller = "Home", 
                          action = "Index", 
                          id = UrlParameter.Optional }
    );
}

So suppose we had a folder on the site root called Markets containing an audio.mp3 file:

假设我们在网站根目录上有一个名为Markets的文件夹,其中包含音频。mp3文件:

\Markets
\Markets\audio.mp3

Assuming the existence of a MarketsController, if we made a request for Markets, it'd be routed to Markets/Index.

假设存在一个MarketsController,如果我们请求市场,它将被路由到Markets/Index。

If we requested /Markets/audio.mp3 we'd get the mp3 file and if we requested Markets/AnythingElse, normal routing would apply.

如果我们要求/市场/音频。mp3我们会得到mp3文件,如果我们要求市场/其他任何东西,正常的路由将适用。