获取完整的请求url,包括控制器中的参数

时间:2022-10-05 20:10:54

How do I get the complete request URL (including query string) in my controller? Is it a matter of concatenating my URL and form parameters or is there a better way.

如何在控制器中获取完整的请求URL(包括查询字符串)?它是连接URL和表单参数的问题,还是有更好的方法?

I checked this question, but it seems not to be applicable to MVC.

我检查了这个问题,但是它似乎不适用于MVC。

Correct me if I'm wrong.

如果我错了请纠正我。

Some more detailed information:

一些更详细的信息:

My call to my SearchController.AdvancedSearch() originates from a form with about 15 optional parameters. Because of the number of parameters and no possible way (I think) to have optional parameters passed in a clean way, I went for a catchall string that is handled in the controller.

我对SearchController.AdvancedSearch()的调用来自一个包含大约15个可选参数的表单。由于参数的数量,而且不可能(我认为)以一种干净的方式传递可选参数,所以我选择了在控制器中处理的集合所有字符串。

Now I want my controller to store its call in a breadcrumb component so that when the crumb is clicked the exact same result can be fetched (with all, but not more, of the arguments included). To do this I need the entire request.URL, including the querystring.

现在,我希望我的控制器将它的调用存储在一个breadcrumb组件中,以便当单击crumb时,可以获取完全相同的结果(包括所有参数,但不包括更多参数)。要做到这一点,我需要整个请求。URL,包括变量的名称。

In Request.RawURL, request.URL etc this query string is not included. In fact currently I do a plain function.

在请求。RawURL,请求。这个查询字符串不包括在内。实际上,我现在做的是一个普通的函数。

String.Format("{0}/{1}", request.Url, request.form)

This gives me some weird results (like submit button values etc), but it works. If there are suggestions on how to make this less of an ugly hack, please do tell.

这给了我一些奇怪的结果(比如提交按钮值等),但它是有效的。如果有人建议如何减少这种丑陋的黑客行为,请告诉我。

I know where to find the current request, but I can't seem to find a raw URL anywhere though. They are all deprived from the querystring and I need that bit too.

我知道在哪里可以找到当前请求,但是我似乎在任何地方都找不到原始URL。它们都被从querystring中删除,我也需要这个位。

My controller updates a collection of URLs in my BreadCrumb component. Therefore it needs the request URL. How do you suggest I take on this problem?

我的控制器更新我的BreadCrumb组件中的url集合。因此它需要请求URL。你建议我如何处理这个问题?

5 个解决方案

#1


37  

You can use Request.Url.PathAndQuery.

您可以使用Request.Url.PathAndQuery。

MVC5: use Request.RequestUri.PathAndQuery

MVC5:使用Request.RequestUri.PathAndQuery

#2


21  

HttpContext.Current.Request.RawUrl
HttpContext.Current.Request.QueryString

As far as your second question. Instead of tracking the url's, query string info, etc, that your user has gone to. Track the controllers and actions called. That would remove the need for the Request object entirely.

至于你的第二个问题。而不是跟踪url,查询字符串信息,等等,你的用户去。跟踪控制器和调用的操作。这样就完全消除了请求对象的需要。

#3


2  

You can get at the current Request object by using:

你可透过以下方法,获取当前的请求对象:

HttpContext.Current.Request

However, I've gotta ask -- why do you want the request URL? By doing this, you've made your controller dependent on the Request object, which will make your unit tests much harder to write.

但是,我必须要问——为什么要请求URL?通过这样做,您已经使您的控制器依赖于Request对象,这将使您的单元测试更难编写。

#4


2  

Request.RawUrl

Use this property is return the all url.

使用此属性返回所有url。

#5


0  

Within the controller, you can access Request.RawUrl.

在控制器中,您可以访问Request.RawUrl。

#1


37  

You can use Request.Url.PathAndQuery.

您可以使用Request.Url.PathAndQuery。

MVC5: use Request.RequestUri.PathAndQuery

MVC5:使用Request.RequestUri.PathAndQuery

#2


21  

HttpContext.Current.Request.RawUrl
HttpContext.Current.Request.QueryString

As far as your second question. Instead of tracking the url's, query string info, etc, that your user has gone to. Track the controllers and actions called. That would remove the need for the Request object entirely.

至于你的第二个问题。而不是跟踪url,查询字符串信息,等等,你的用户去。跟踪控制器和调用的操作。这样就完全消除了请求对象的需要。

#3


2  

You can get at the current Request object by using:

你可透过以下方法,获取当前的请求对象:

HttpContext.Current.Request

However, I've gotta ask -- why do you want the request URL? By doing this, you've made your controller dependent on the Request object, which will make your unit tests much harder to write.

但是,我必须要问——为什么要请求URL?通过这样做,您已经使您的控制器依赖于Request对象,这将使您的单元测试更难编写。

#4


2  

Request.RawUrl

Use this property is return the all url.

使用此属性返回所有url。

#5


0  

Within the controller, you can access Request.RawUrl.

在控制器中,您可以访问Request.RawUrl。