简体   繁体   English

ASP.NET MVC 4 RedirectToAction无法正常工作

[英]ASP.NET MVC 4 RedirectToAction doesn't work properly

I created an MVCApplication and it works in a directory on my server like this: 我创建了一个MVCApplication,它可以在服务器上的目录中像这样工作:

 http://www.mywebsite.com/MyApp/

When I use RedirectToAction like this; 当我像这样使用RedirectToAction

 return RedirectToAction("Index", "Home");

It goes to; 它去;

 http://www.mywebsite.com/Home/Index

But I want to redirect to; 但是我想重定向到;

 http://www.mywebsite.com/MyApp/Home/Index);

How can I resolve it? 我该如何解决?

Edit--- 编辑 - -

My routeconfig is like this; 我的routeconfig是这样的;

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
); 

These redirects follow your route configuration. 这些重定向遵循您的路由配置。 The default route is {siteUrl}/{controller}/{action}, which is why when you give it the "Index" action and the "Home" controller, you get the URL you're seeing. 默认路由为{siteUrl} / {controller} / {action},这就是为什么当您将其赋予“ Index”操作和“ Home”控制器时,得到所看到的URL的原因。

You need to specify a new route similar to this: 您需要指定与此相似的新路线:

MapRoute("CustomRoute", "MyApp/{controller}/{action}";

You can then redirect to it like this: 然后您可以像这样重定向到它:

RedirectToRoute("CustomRoute", new {Controller = "Home", Action = "Index" });

EDIT to clarify for the comments - 编辑以澄清意见-

It is also possible to solve this using IIS to make MyApp an application. 也可以使用IIS解决此问题,以使MyApp成为应用程序。 This is the right solution only if MyApp is the only value you ever want at the beginning of your route for this site. 仅当 MyApp是您在此站点的路线开始时想要的唯一值时,这才是正确的解决方案。 If, for example, you sometimes need MyApp/{controller}/{action} and other times you need OtherSubfolder/{controller}/{action}, you need to use the routes as I have outlined above (or use areas) instead of just updating IIS. 例如,如果有时您需要MyApp / {controller} / {action},而有时又需要OtherSubfolder / {controller} / {action},则需要使用我上面概述的路由(或使用区域),而不是只是更新IIS。

I am assuming you want the solution using MVC, so that is what is here, but you should also consider the IIS application if that's the right solution for you. 我假设您想要使用MVC的解决方案,这就是这里的内容,但是如果这是适合您的解决方案,那么您还应该考虑IIS应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM