更改ASP.NET MVC或IIS中的URL根路径

时间:2022-09-03 20:12:24

I'm working with a server with a custom URL: http://example.com/site/ and as you can see I need to change the default root path ("/") of my application to prevent 404 errors. Using IIS rewrite module with a outbound rule seems to work, with all html links and references being converted properly to query the internal website. The problem is when a Redirect() or RedirectToAction() method is used in my controllers, the internal name of the website is dropped so a 404 is caused. This is my IIS outbound rule:

我正在使用带有自定义网址的服务器:http://example.com/site/,您可以看到我需要更改应用程序的默认根路径(“/”)以防止404错误。使用具有出站规则的IIS重写模块似乎有效,所有html链接和引用都正确转换为查询内部网站。问题是当我的控制器中使用Redirect()或RedirectToAction()方法时,网站的内部名称将被删除,从而导致404。这是我的IIS出站规则:

<rewrite>
  <outboundRules>
    <rule name="Add path prefix to urls" stopProcessing="true">
      <match filterByTags="A, Form, Img, Link, Script" pattern="^/(.*)" />
      <action type="Rewrite" value="/site{R:0}" />
    </rule>
  </outboundRules>
</rewrite>

So to clarify: I have http://example.com/site/ when a redirection occurs to Account/Login, it becomes http://example.com/account/login instead of http://example.com/site/account/login . I guess the RouteConfig has to be tinkered with but I don't know how, or if I can do this in IIS. I have the following in my RouteConfig class:

所以澄清一下:我在http://example.com/site/上进行了帐户/登录重定向时,它变成了http://example.com/account/login而不是http://example.com/site/帐号登录 。我想RouteConfig必须修补,但我不知道如何,或者我是否可以在IIS中执行此操作。我在RouteConfig类中有以下内容:

routes.MapRoute(
    name: "SiteRoot",
    url: "site/{controller}/{action}",
    defaults: new { controller = "Home", action = "Index" }
);

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

Thank you.

1 个解决方案

#1


0  

Assuming the issue is only with pages protected by the authorize annotation, you probably need to update the /App_Start/Startup.Auth.cs file to change the login path. You're looking for LoginPath = new PathString("/Account/Login"), which you'll change to LoginPath = new PathString("/site/Account/Login"),. In the MVC 5 starter site I tested in this was on line #28.

假设问题仅在受授权注释保护的页面上,您可能需要更新/App_Start/Startup.Auth.cs文件以更改登录路径。您正在寻找LoginPath = new PathString(“/ Account / Login”),您将更改为LoginPath = new PathString(“/ site / Account / Login”),.在我测试的MVC 5启动器站点中,在第28行。

In my testing with your redirect rule and route configuration normal RedirectToActions worked fine, but if you're having trouble you might try removing the "Default" route mapping so that URL structure isn't selected by the router when trying to redirect.

在我使用重定向规则和路由配置进行测试时,正常RedirectToActions工作正常,但是如果遇到问题,可以尝试删除“默认”路由映射,以便路由器在尝试重定向时不会选择URL结构。

#1


0  

Assuming the issue is only with pages protected by the authorize annotation, you probably need to update the /App_Start/Startup.Auth.cs file to change the login path. You're looking for LoginPath = new PathString("/Account/Login"), which you'll change to LoginPath = new PathString("/site/Account/Login"),. In the MVC 5 starter site I tested in this was on line #28.

假设问题仅在受授权注释保护的页面上,您可能需要更新/App_Start/Startup.Auth.cs文件以更改登录路径。您正在寻找LoginPath = new PathString(“/ Account / Login”),您将更改为LoginPath = new PathString(“/ site / Account / Login”),.在我测试的MVC 5启动器站点中,在第28行。

In my testing with your redirect rule and route configuration normal RedirectToActions worked fine, but if you're having trouble you might try removing the "Default" route mapping so that URL structure isn't selected by the router when trying to redirect.

在我使用重定向规则和路由配置进行测试时,正常RedirectToActions工作正常,但是如果遇到问题,可以尝试删除“默认”路由映射,以便路由器在尝试重定向时不会选择URL结构。