asp.net-mvc 获取完整的请求 url,包括控制器中的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/261345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get full request url, including parameters in controller
提问by Boris Callens
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 thisquestion, 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.
我对 my 的调用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.
现在我希望我的控制器将它的调用存储在面包屑组件中,这样当点击面包屑时,可以获取完全相同的结果(包括所有参数,但不是更多参数)。为此,我需要整个 request.URL,包括 querystring。
In Request.RawURL, request.URLetc this query string is not included. In fact currently I do a plain function.
在Request.RawURL,request.URL等不包括在此查询字符串。事实上,目前我做了一个普通的功能。
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。他们都被剥夺了查询字符串,我也需要那一点。
My controller updates a collection of URLs in my BreadCrumbcomponent. Therefore it needs the request URL. How do you suggest I take on this problem?
我的控制器更新了我BreadCrumb组件中的一组 URL 。因此它需要请求 URL。你如何建议我解决这个问题?
回答by Serhiy
You can use Request.Url.PathAndQuery.
您可以使用Request.Url.PathAndQuery.
MVC5: use Request.RequestUri.PathAndQuery
MVC5:使用Request.RequestUri.PathAndQuery
回答by Jeff Sheldon
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、查询字符串信息等。跟踪调用的控制器和操作。这将完全消除对 Request 对象的需要。
回答by Dino Liu
Request.RawUrl
Use this property is return the all url.
使用此属性是返回所有 url。
回答by Roger Lipscombe
You can get at the current Request object by using:
您可以使用以下方法获取当前的 Request 对象:
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 对象,这将使您的单元测试更难编写。
回答by Haacked
Within the controller, you can access Request.RawUrl.
在控制器中,您可以访问Request.RawUrl.

