使用Url.RouteUrl时,区域缺失

发布于 2025-01-01 12:25:10 字数 618 浏览 0 评论 0原文

我怀疑我做错了。

由于各种原因,我的应用程序强制用户在登录后立即做出一些选择。为了确保他们输入必要的数据,我重写了基本控制器类中的 OnActionExecuting 方法,以拦截在输入此数据之前执行操作的任何尝试,并将用户重定向到必要的页面。我使用以下代码保留他们尝试执行的操作的 url:

url = Url.RouteUrl("Default", filterContext.RouteData.Values);

(filterContext 是一个 ActionExecutingContext 对象,也是 OnActionExecuting 的一个参数。)

我遇到的问题是,如果操作与区域,我得到的网址不反映该区域。

我从其他帖子中了解到,我可以从 RouteData 的 DataTokens 集合中获取区域名称。但我不确定通过它的最佳方式。我想我可以检索它并使用 RouteValueDictionary.Add 方法将其添加到 RouteData.Values (假设 Values 此时不是只读的;我不知道),但这感觉有点......奇怪,就像不知何故,人们忽略了这一点。

这真的是应该的方式吗?其他地方是否有问题,我的 RouteData.Values 中缺少该区域?

I suspect I'm doing this wrong.

For various reasons, my app forces the user to make some choices right after login. In order to ensure that they enter the necessary data, I override the OnActionExecuting method in a base controller class to intercept any attempt at executing an action before this data has been entered, and redirect the user to the necessary page. I preserve the url of the action they were attempting to execute with the following code:

url = Url.RouteUrl("Default", filterContext.RouteData.Values);

(filterContext is an ActionExecutingContext object, and a parameter of OnActionExecuting.)

The problem I'm having is that, if the action was associated with a controller in an area, the url I get doesn't reflect the area.

I understand from other posts that I can get the area name from the DataTokens collection of RouteData. But I'm uncertain of the best way to pass it. I suppose I could retrieve it and use the RouteValueDictionary.Add method to add it to RouteData.Values (assuming Values is not read-only at that point; I don't know), but that feels a bit ... odd, like somehow the point is being missed.

Is this really the way this is supposed to be done? Is there something wrong elsewhere, that Area is absent from my RouteData.Values?

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

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

发布评论

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

评论(1

放肆 2025-01-08 12:25:10

我只需将其从过滤器中的数据令牌中取出并将其添加到路由值中即可。您可以使用 RouteValues.Add 来实现:

if (filterContext.RouteData.DataTokens.ContainsKey("area"))
    filterContext.RouteData.Values.Add("area", 
        filterContext.RouteData.DataTokens["area"]);

MVC2 中添加了区域功能,我想这是 MVC1 中没有的副作用。但是,只要您的 RouteValues 包含“区域”键,UrlHelper.RouteUrl 就应该为该区域生成正确的 URL。

I would just take it out of the data tokens in the filter and add it to route values. You can do it with RouteValues.Add:

if (filterContext.RouteData.DataTokens.ContainsKey("area"))
    filterContext.RouteData.Values.Add("area", 
        filterContext.RouteData.DataTokens["area"]);

The areas feature was added in MVC2, and I imagine this is a side effect of it not being in MVC1. However, as long as your RouteValues contains an "area" key, UrlHelper.RouteUrl should generate the correct URL for the area.

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