【问题标题】:ASP .NET MVC RedirectoToAction ignores the application where is locatedASP .NET MVC RedirectoToAction 忽略所在的应用程序
【发布时间】:2013-08-06 15:07:40
【问题描述】:

我有一个 MVC 应用程序部署在我的服务器中的虚拟目录中,例如:

http://localhost/myapp/

其中“myapp”是虚拟目录

在我的登录视图中,位于

"http://localhost/myapp/user/login", 

我使用RedirectToAction("Index", "Home") 重定向到索引,似乎应用程序尝试重定向到

"http://localhost/home/index" 

而不是

"http://localhost/myapp/home/index".

当应用程序位于 IIS 网站的根目录时,它可以工作,但在给定的情况下不能工作。

我错过了配置应用程序根目录的方法吗?

设置:Microsoft Visual Studio Express 2012 for Web、Windows 7 下的 IIS 7、应用程序池 ASP .NET v4.0

【问题讨论】:

  • 您是否将虚拟目录设置为应用程序?

标签: asp.net-mvc iis-7


【解决方案1】:

我 99% 肯定这样做:

return RedirectToAction("Index", "Home")

是相对应用程序根目录,意味着无论虚拟目录设置或应用程序位于何处,它都应重定向到您所在的应用程序。我的意思是想想噩梦,否则每次将应用程序移动到不同的虚拟目录时,都需要更新 global.asax 或 web.config 文件???荒谬的!我们的设置与您的设置相同,并且我们对“应用程序跳跃”没有任何问题。

您确定是 RedirectToAction 造成的吗?是不是你有类似的东西:

@Url.Content("/Home/Index")

在这种情况下,您会遇到此问题,您可以通过以下方式轻松解决此问题:

@Url.Content("~/Home/Index")

~ 符号使其成为应用程序根相对...

【讨论】:

  • 我不确定这是否正确,没有自己测试过,但它是有道理的! +1
  • 必须是return RedirectToAction("Index", "Home")
【解决方案2】:

这是正确的功能。默认情况下,MVC 会将路由计算为控制器/动作。

如果你想这样做,你需要添加一个路由到Global.asax

//this is your new route which needs to be ABOVE the detault
routes.MapRoute(
// Route name
"myapp_Default",
// Url with parameters
"myapp/{controller}/{action}/{id}",
// Parameter defaults
new { action = "index", id = UrlParameter.Optional });


//This is the default route that should already be there.
routes.MapRoute(
// Route name
"Default",
// Url with parameters
"{controller}/{action}/{id}",
// Parameter defaults
new { action = "index", id = UrlParameter.Optional });

scott gu 的blog 中有关路由的更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多