高级 ASP.NET MVC 路由场景

发布于 2024-09-11 12:29:59 字数 573 浏览 4 评论 0原文

我有一个 ASP.NET MVC 应用程序,具有以下部署要求:

URL 结构必须类似于:

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

我想我想要做的是在MVC路由之前拦截URL handler 接手它,删除 [enterprise]/[community] 部分,然后允许 MVC 继续处理,就好像原始 URL 不包含这两个段一样。

原因如下:

该应用程序向多个客户(企业)公开多个门户,并且企业内的每个社区都有自己的用户群。这种方案也可以通过将一个应用程序实例(二进制文件、内容、web.config)物理部署到每个 [community] 目录中来实现,但出于逻辑和性能方面的原因,我认为我们不想走这条路。所以我试图通过路由技巧来虚拟化它。

任何有关如何实施此计划或替代解决方案的建议将不胜感激。

如果这有什么区别的话,我们使用的是 IIS 7。

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.

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

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

发布评论

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

评论(2

So尛奶瓶 2024-09-18 12:30:00

这是使用 IIS 重写模块的可能解决方案。这可能不是最好的方法,但可能有效。 MVC 路由中是否有更简单/更好的选项?没有把握。我自己才刚刚开始这样做。

使用“http://server.com/app/enterprise/community/controller/action /”为例。

会发生什么:

  1. 从 URL 中删除字符串。新的
    网址:
    http://server.com/controller/action/
  2. 将用户重定向到新 URL。用户的
    浏览器现在显示:
    http://server.com/controller/action/
  3. 获取新 URL 并尝试
    重建它以获取正确的
    内容。用户浏览器显示:
    http://server.com/controller/action/ ;
    IIS 返回:
    http://server.com/app/enterprise/community/controller/action/< /a>

安装 IIS 重写模块后,所有这些都将位于 web.config 中:

<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>

注意:只是快速完成此操作,尚未测试。

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.

岁月打碎记忆 2024-09-18 12:29:59

您可以在默认路由之前使用以下路由,

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

然后可以忽略操作方法中的 {enterprise} 和 {community} 参数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文