C# 虚拟目录中的 ASP.NET MVC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/182993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC in a virtual directory
提问by Evil Andy
I have the following in my Global.asax.cs
我的 Global.asax.cs 中有以下内容
routes.MapRoute(
"Arrival",
"{partnerID}",
new { controller = "Search", action = "Index", partnerID="1000" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
My SearchController looks like this
我的 SearchController 看起来像这样
public class SearchController : Controller
{
// Display search results
public ActionResult Index(int partnerID)
{
ViewData["partnerID"] = partnerID;
return View();
}
}
and Index.aspx simply shows ViewData["partnerID"] at the moment.
和 Index.aspx 目前只显示 ViewData["partnerID"]。
I have a virtual directory set up in IIS on Windows XP called Test.
我在 Windows XP 上的 IIS 中设置了一个名为 Test 的虚拟目录。
If I point my browser at http://localhost/Test/then I get 1000 displayed as expected. However, if I try http://localhost/Test/1000I get a page not found error. Any ideas?
如果我将浏览器指向http://localhost/Test/,则会按预期显示 1000。但是,如果我尝试http://localhost/Test/1000我得到一个页面未找到错误。有任何想法吗?
Are there any special considerations for running MVC in a virtual directory?
在虚拟目录中运行 MVC 有什么特别的考虑吗?
采纳答案by Amith George
IIS 5.1 interprets your url such that its looking for a folder named 1000 under the folder named Test. Why is that so?
IIS 5.1 解释您的 url,以便在名为 Test 的文件夹下查找名为 1000 的文件夹。为什么呢?
This happens because IIS 6 only invokes ASP.NET when it sees a “filename extension” in the URL that's mapped to aspnet_isapi.dll (which is a C/C++ ISAPI filter responsible for invoking ASP.NET). Since routing is a .NET IHttpModule called UrlRoutingModule, it doesn't get invoked unless ASP.NET itself gets invoked, which only happens when aspnet_isapi.dll gets invoked, which only happens when there's a .aspx in the URL. So, no .aspx, no UrlRoutingModule, hence the 404.
发生这种情况是因为 IIS 6 只有在 URL 中看到映射到 aspnet_isapi.dll(负责调用 ASP.NET 的 C/C++ ISAPI 过滤器)的“文件扩展名”时才会调用 ASP.NET。由于路由是一个名为 UrlRoutingModule 的 .NET IHttpModule,它不会被调用,除非 ASP.NET 本身被调用,这只会在调用 aspnet_isapi.dll 时发生,这只会在 URL 中有 .aspx 时发生。所以,没有 .aspx,没有 UrlRoutingModule,因此是 404。
Easiest solution is:
最简单的解决方法是:
If you don't mind having .aspx in your URLs, just go through your routing config, adding .aspx before a forward-slash in each pattern. For example, use {controller}.aspx/{action}/{id} or myapp.aspx/{controller}/{action}/{id}. Don't put .aspx inside the curly-bracket parameter names, or into the ‘default' values, because it isn't really part of the controller name - it's just in the URL to satisfy IIS.
如果您不介意在您的 URL 中使用 .aspx,只需检查您的路由配置,在每个模式中的正斜杠之前添加 .aspx。例如,使用 {controller}.aspx/{action}/{id} 或 myapp.aspx/{controller}/{action}/{id}。不要将 .aspx 放在花括号参数名称中,或者放在“默认”值中,因为它实际上并不是控制器名称的一部分——它只是在 URL 中以满足 IIS。
Source: http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/
来源:http: //blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/
回答by NotMe
There are a number of considerations when using virtual directories in your application.
在您的应用程序中使用虚拟目录时有许多注意事项。
One is particular is that most browsers will not submit cookies that came from one virtual directory to another, even if the apps reside on the same server.
一个特别之处在于大多数浏览器不会将来自一个虚拟目录的 cookie 提交到另一个虚拟目录,即使应用程序驻留在同一台服务器上。
回答by Hrvoje Hudo
Try set virtual path: right click on mvc project, properties, web tab, there enter appropriate location.
尝试设置虚拟路径:右键单击 mvc 项目,属性,web 选项卡,在那里输入适当的位置。
回答by Sean Carpenter
If you are doing this on Windows XP, then you're using IIS 5.1. You need to get ASP.Net to handle your request. You need to either add an extension to your routes ({controller}.mvc/{action}/{id}) and map that extension to ASP.Net or map all requests to ASP.Net. The http://localhost/Testworks because it goes to Default.aspx which is handled specially in MVC projects.
如果您在 Windows XP 上执行此操作,那么您使用的是 IIS 5.1。您需要让 ASP.Net 来处理您的请求。您需要向路由添加扩展({controller}.mvc/{action}/{id})并将该扩展映射到 ASP.Net 或将所有请求映射到 ASP.Net。该HTTP://本地主机/测试工作,因为它关系到它在MVC项目特殊处理的Default.aspx。
Additionally, you need to specify http://localhost/Test/Search/Index/1000. The controller and action pieces are not optional if you want to specify an ID.
此外,您需要指定http://localhost/Test/Search/Index/1000。如果要指定 ID,控制器和操作部分不是可选的。