ASP MVC 3 - 如何定义 url 基的动态值?

发布于 2024-12-06 07:38:46 字数 427 浏览 5 评论 0原文

我想向 URL 的基础“显示”动态值,因此 URL 将如下所示:host.com/SOME_VALUE/{area}/{controller}/{action}。

因此,如果请求的 url 没有第一个值(动态值):host.com/{area}/{controller}/{action},则应该调用正确的操作,但是当呈现视图时(或者可能在重定向时)发生)正确的 url 应返回正确的第一个值。

此解决方案仅适用于在 url 中显示标识用户登录的指定值,例如用户名或公司名称,或与当前会话相关的任何其他值,该值不会用于限制对操作,因此两个网址都应该有效,并在同一会话上调用相同的操作:

host.com/{area}/{controller}/{action} host.com/some_value/{area}/{controller}/{action}

有什么建议吗?

I would like to "show" a dynamic value to the base of the URL, so the url would be like this: host.com/SOME_VALUE/{area}/{controller}/{action}.

So, if a url without the first value (the dynamic value) is requested: host.com/{area}/{controller}/{action} the correct action should be called but when a view is rendered (or maybe when a redirect occurs) the correct url should be returned with the correct first value.

This solution would be useful only to show in the url a specified value the identifies the user logon, like the username or maybe the company name, or any other value related to the current session, this value won't be used to restricts access to the actions, so the both urls should be valid and call the same action on then same session:

host.com/{area}/{controller}/{action}
host.com/some_value/{area}/{controller}/{action}

Any suggestions ?

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

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

发布评论

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

评论(1

能怎样 2024-12-13 07:38:46

这完全未经测试,但我认为它会起作用...

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "MyRoute", // Route name
        "{somevalue}/{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

}

当使用 somevalue 部分时,是否可以向 URL 添加额外的静态值?即host.com/users/some_value/area/controller/action。这将使路线映射变得简单,因为您所需要的只是:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "LoggedInUsers", // Route name
        "Users/{somevalue}/{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

}

This is completely untested but i'd have thought it would work...

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "MyRoute", // Route name
        "{somevalue}/{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

}

Would it be possible to add an additional static value to the URL when the somevalue part is used? i.e. host.com/users/some_value/area/controller/action. It would make the route mapping simple as all you'd need is:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "LoggedInUsers", // Route name
        "Users/{somevalue}/{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

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