ASP.NET MVC 3:将应用程序移至虚拟目录中。我必须改变什么?

发布于 2024-12-14 07:27:20 字数 550 浏览 3 评论 0原文

我一直在开发 MVC 3 应用程序。我使用的是 VS 2010 的内置 Web 服务器。

今天,由于各种原因,我被要求将其移动到虚拟目录中并在 IIS 7 下运行,仍然在我的开发 PC 上。

现在它的 URL 是 localhost/MyVirtualDirectory 而不是 localhost:12345,我需要更改什么才能使路由正常工作,以及在哪里?

我没有使用任何原始 HTML 锚标记或重定向,只是使用 @Html.ActionLink 等。根据我所读到的内容,如果我一直以 MVC 方式做事,那么这种变化应该是透明的。

但一开始,身份验证后重定向就失败了。身份验证成功后,它应该返回

this.RedirectToAction("index", "Home")

您猜对了的结果:重定向到 /Home,而不是 /MyVirtualDirectory/Home。这失败了。

所以缺少一些需要设置的东西。它是什么?

I have been working on an MVC 3 app. I was using VS 2010's built-in web server.

Today, for various reasons, I was asked to move it into a virtual directory and run it under IIS 7, still on my development PC.

Now that its URL is localhost/MyVirtualDirectory as opposed to localhost:12345, what do I need to change to make routing work, and where?

I'm not using any raw HTML anchor tags or redirects, just @Html.ActionLink and so on. According to what I've read, if I've been doing things the MVC way, this change should have been transparent.

But right at the beginning, the post-authentication redirection fails. On successful authentication, it's supposed to return the result of

this.RedirectToAction("index", "Home")

You guessed it: instead of /MyVirtualDirectory/Home the redirection goes to /Home. Which fails.

So something is missing that needs to be set up. What is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2024-12-21 07:27:20

在 IIS 中,选择您的虚拟目录并“转换为应用程序”。另外,如果您在 Global.asax 中使用默认路由映射,它应该是这样的:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

推理:如果您将 MVC 应用程序放在另一个应用程序的子目录中,那么 IIS 将考虑该其他应用程序的根目录,而不是MVC 应用程序的根目录。如果这是您想要的行为(不太可能),那么您需要修改 Global.asax 以考虑到这一点:

routes.MapRoute(
    "Default", // Route name
    "MyVirtualDirectory/{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

In IIS, choose your virtual directory and "Convert to Application." Also, if you are using the default route map in your Global.asax it should read something like this:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Reasoning: If you put your MVC application in a sub-directory of another application then IIS will consider the root of that other application instead of the root of your MVC application. If that is the behavior that you want (unlikely) then you need to modify your Global.asax to take that into account:

routes.MapRoute(
    "Default", // Route name
    "MyVirtualDirectory/{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文