How to select a query string in ASP.Net MVC4 Razor

I have a URL like this

http://localhost:1243/home/index/?skip_api_login=1&api_key=145044622175352&signed_next=1

Now homemy controller indexis my action.

But please tell me how I can select skip_api_login, api_key, signed_next in the razor asp.net MVC4.

I want to use these values ​​in the controller and views. Please tell me how to choose them.

+4
source share
1 answer

You could make your controller action take them as parameters, and the connecting device models its values ​​from the query string:

public ActionResult Index(string skip_api_login , string api_key, int signed_next)
{
    ...
}

or an event is better, write a view model:

public class MyViewModel
{
    public string Skip_api_login { get; set; }
    public string Api_key { get; set; }
    public int Signed_next { get; set; }
}

so that your index action is taken as a parameter:

public ActionResult Index(MyViewModel model)
{
    ...
    return View(model);
}

, :

@model MyViewModel
...
@Html.DisplayFor(x => x.Skip_api_login)
@Html.DisplayFor(x => x.Api_key)
@Html.DisplayFor(x => x.Signed_next)

, Request:

@Request["skip_api_login"]

, , . , , . , , viewdatas, viewbags, , . ASP.NET MVC : .

+6

Source: https://habr.com/ru/post/1526827/


All Articles