Raj
Raj

Reputation: 195

How to pass a URL as a query string parameter in MVC

does anyone know how to pass a URL as a query string parameter and then get the URl in HttpGet method as a parameter ?

Upvotes: 4

Views: 7510

Answers (3)

Raj
Raj

Reputation: 195

Thanks so much for all the answers. Finally I got it sorted. Please refer to my fix below:

[Route("api/[controller]")]
public class UrlController : Controller
{
    [HttpGet("{*longUrl}")]
    public string ShortUrl(string longUrl)
    {
        var test = longUrl + Request.QueryString;

        return JsonConvert.SerializeObject(GetUrlToken(test));
    }

Upvotes: 7

Sky Fang
Sky Fang

Reputation: 1101

just like this?

<a href="/Home/[email protected]("http://stackoverflow.com")">current url with UrlEncode</a>

[HttpGet]
public ActionResult Index(string url = null)
{
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
    return View();
}

Upvotes: 2

SIDDU HUSSAIN
SIDDU HUSSAIN

Reputation: 47

HttpUtility.UrlEncode should do the job for you

404 might be because the application is not in running mode

host you application and try it in local it should be working as needed

i was trying as http://localhost:11331/Home/?id=http%3A%2F%2Flocalhost%3A11331%2F

and everything is working fine even the redirection to the launch screen

or else post the complete URL you are getting so that i can help you

Upvotes: 1

Related Questions