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

十年热恋 提交于 2019-12-10 18:32:19

问题


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


回答1:


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));
    }



回答2:


just like this?

<a href="/Home/Index?url=@HttpUtility.UrlEncode("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();
}



回答3:


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



来源:https://stackoverflow.com/questions/30648222/how-to-pass-a-url-as-a-query-string-parameter-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!