开发者

MVC3 URL as parameter

开发者 https://www.devze.com 2023-03-16 02:09 出处:网络
I want to create a MVC application that recieves a URL as parameter. How to achieve that? Recieve the parameter directly on the URL?

I want to create a MVC application that recieves a URL as parameter. How to achieve that?

  1. Recieve the parameter directly on the URL?
  2. Create Get and Post methods passing internally the URL from the Get one to the Post one?

I tried the first option but still cannot recieve URLs like http://website/controller/method/http://otherurl.com

Do I have to create a ne开发者_运维技巧w route to achieve this?

Thanks everyone.


I would recommend you using a query string parameter and also make sure the value is properly url encoded:

http://website/controller/method?url=http%3A%2F%2Fotherurl.com

and inside the action:

public ActionResult Method(string url)
{
    // The url parameter here will equal to "http://otherurl.com"
    ...
}


URLs consist of two parts, the first part is the domain (I think) and what matters here is the second which is called the query string. (the 1st part is mandatory, the 2nd is not).

this is an example:

http://your-domain-here/stuff?page=1

Now page is a query string variable. You have to notice the ? which separates the two parts of the Url, I don't see one in your URL, so IMO, MVC routing engine will try to match this whole Url with a registered route (which will not be found).

No, I would say, you don't have to create a new route, new routes are created when we're in need of a new "Path" but in your case you only need to improve the URL to separate the route off the query string.

Hope that helps.


You would have to create another route, something like:

routes.MapRoute( "UrlByParam", //Route Name
"{controller}/{action}/{url}", //Url Pattern
new { controller = "DefaultController", action = "DefaultAction" }); //Defaults

And you also have to encode your URL, so,

http://website/controller/method/http://otherurl.com

would become

http://website/controller/method/http%3A%2F%2Fotherurl.com
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号