为什么很难在控制器上下文之外获取路由值? [英] Why is it so hard to get route values outside controller context?

查看:71
本文介绍了为什么很难在控制器上下文之外获取路由值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这是怎么回事,为什么在控制器的 Request 中获取路由值这么容易,却几乎不可能在 HttpContext.Current.Request

I don't understand what's the deal behind this, why is it so easy to get route values inside Request of controller but nearly impossible to do the same thing on HttpContext.Current.Request?

也许我只是不知道更好的方法,它的存在。有人可以确认这是在控制器外部获取路线数据的唯一方法吗?

Maybe I just don't know a better way and it exists. Can someone confirm that this is the only way to get route data outside controller?

[Route("{id}"), HttpGet]
public IHttpActionResult Test()
{
    // Simple and easy
    var route1 = Request.GetRouteData().Values["id"];

    // Wat. This is also ~6 times slower
    var routeValues = (IHttpRouteData[]) HttpContext.Current.Request.RequestContext.RouteData.Values["MS_SubRoutes"];
    var route2 = routeValues.SelectMany(x => x.Values).Where(x => x.Key == "id").Select(x => x.Value).FirstOrDefault();

    return Ok(route1 == route2); // true
}


推荐答案

第一部分

问题的答案-


为什么在控制器的Request中获取路由值这么容易,但是
几乎不可能在HttpContext.Current.Request上做同样的事情?

Why is it so easy to get route values inside Request of controller but nearly impossible to do the same thing on HttpContext.Current.Request?

我不会详细介绍类实现的所有细节。让我们只考虑MVC体系结构中的Model,View和Controller的责任。您会看到,只有Controller负责将路由映射到视图,反之亦然。那是MVC体系结构中唯一需要路由的部分。因此,很容易理解为什么在Controller内这么容易找到路由值。

I am not going in all the details of class implementation and others. Lets just consider the responsibility of Model, View and Controller in an MVC architecture. And you can see that only the Controller has the responsibility to map routes to views and vice versa. That is the only part of the MVC architecture that needs the routes. So it can readily be understood that why it is so easy to find route values inside Controller.

现在介绍实现细节。是的,当然,MS人员已经做了很多工作,以使其可以在控制器内轻松访问,只是一个简单的Property。但是,如果您希望在控制器外部使用它,您仍然可以拥有它,但这是一项艰巨的工作。相当可观,因为除非您试图弄乱体系结构的抽象层,否则您将永远不需要控制器外部的路由。

Now comes the implementation details. Yeah, of course MS folks have done a great deal of work to make it easily accessible inside a controller, just a simple Property. But if you want it outside controllers you can still have it, but it is a hard work. That is considerable, cause unless you are trying to mess up the abstraction layer of the architecture, you are not gonna need the route outside the controller anyway.

部分两个

现在是第二个答案-


可以有人确认这是在控制器外部获取路由数据的唯一方法吗?

Can someone confirm that this is the only way to get route data outside controller?

当然还有其他方法。实际上,我本人不明白为什么您会通过这么辛苦的工作来获取路线价值?您不是在控制器中编写动作方法吗?如果是这样,那为什么不只是将其用作参数呢? -

Of course there are other ways. In fact I myself can't understand why are you getting route values by doing so hard work? Aren't you writing your action methods in controllers? If so then why not just use it as parameter? -

[Route("{id}"), HttpGet]
public IHttpActionResult Test(int? id) //the easiest way to get route value for {id}
{
    // Simple and easy
    var route1 = Request.GetRouteData().Values["id"];

    // Wat. This is also ~6 times slower
    var routeValues = (IHttpRouteData[]) HttpContext.Current.Request.RequestContext.RouteData.Values["MS_SubRoutes"];
    var route2 = routeValues.SelectMany(x => x.Values).Where(x => x.Key == "id").Select(x => x.Value).FirstOrDefault();

    return Ok(route1 == route2 == id.Value); // true //should be true, and of course you should add null check since it is int? not int. But that is not significant here so I didn't do it.
}

这篇关于为什么很难在控制器上下文之外获取路由值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆