[英]How to get url in ASP.NET MVC controller
Does anyone have a nice way of getting the url of the current page in the controller?有没有人在 controller 中获取当前页面的 url 的好方法?
More specifically I need a specific query parameter.更具体地说,我需要一个特定的查询参数。
My action looks like this;我的动作是这样的; do I need to put in an argument?
我需要提出论点吗?
public ActionResult Random()
{
RandomMovieViewmodel rmvm = new RandomMovieViewmodel();
return View(rmvm);
}
It sounds like you should be using a parameter for this.听起来您应该为此使用参数。 For example, if you needed an "id" value you could make your method like this:
public ActionResult Random(int id)
.例如,如果您需要一个“id”值,您可以使您的方法如下:
public ActionResult Random(int id)
。 So when you call the controller, like so mysite.com/Random?id=1
, the value in the url is automatically bound to your parameter of the same name.因此,当您调用 controller 时,例如
mysite.com/Random?id=1
,url 中的值会自动绑定到您的同名参数。
However, if you really feel like parsing the url, you can call Request.Url
or, if you are using.Net Core, call Request.GetDisplayUrl()
.但是,如果您真的想解析 url,您可以调用
Request.Url
,或者,如果您使用.Net Core,请调用Request.GetDisplayUrl()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.