Advanced ASP.NET MVC routing scenario

邮差的信 提交于 2020-01-16 08:35:23

问题


I have an ASP.NET MVC app with the following deployment requirements:

The URL structure must be something like:

http://server/app/[enterprise]/[communinty]/{controller}/{action}/...

What I think I want to be able to do is intercept the URL before the MVC route handler gets its hands on it, remove the [enterprise]/[community] parts, and then allow MVC to continue processing as if the original URL had not contained those two segments.

Here's why:

The application exposes multiple portals to multiple customers (enterprises), and each community within an enterprise has its own user population. This kind of scheme could also be served by physically deploying one application instance (binaries,content,web.config) into each [community] directory, but for logistical and performance reasons, I don't think we want to go down this path. So I'm trying to virtualize it through routing tricks.

Any suggestions on how to go about this scheme, or alternate solutions would be appreciated.

We are on IIS 7, if that makes any difference.


回答1:


You can use the following route before the default route

routes.MapRoute(
    null,
    "{enterprise}/{community}/{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

You can then ignore {enterprise} and {community} parameters in your action methods.




回答2:


Here is a possible solution with IIS Rewrite module. It may not be the best approach, but it may work. Is there an easier/better option within the MVC routing? Not sure. Only just started doing that myself.

Using "http://server.com/app/enterprise/community/controller/action/" as an example.

What happens:

  1. Strips the string out of the URL. New URL: http://server.com/controller/action/
  2. Redirects the user to new URL. User's browser now shows: http://server.com/controller/action/
  3. Takes the new URL and tries to rebuild it to grab the correct content. User's browser shows: http://server.com/controller/action/ ; IIS returns: http://server.com/app/enterprise/community/controller/action/

All of this would be in the web.config once the IIS rewrite module is installed:

<rewrite>
    <rules>
        <clear />

        <rule name="Redirect to remove Offending String" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
            <match url="server.com/app/enterprise/community*" />
            <action type="Redirect" url="/{R:1}" />
            <conditions logicalGrouping="MatchAll">
                <add input="{SERVER_NAME}" pattern="*server.com*" />
            </conditions>
        </rule>

        <rule name="Rewrite to get Original Content" enabled="true" patternSyntax="Wildcard" stopProcessing="false">
            <match url="*" />
            <conditions logicalGrouping="MatchAll">
                <add input="{SERVER_NAME}" pattern="*server.com*" />
            </conditions>
            <action type="Rewrite" url="app/enterprise/community{R:1}" />
        </rule>

    </rules>
</rewrite>

Note: Just did this quick, haven't tested.



来源:https://stackoverflow.com/questions/3321750/advanced-asp-net-mvc-routing-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!