Answered by:
Read MVC URL

Question
-
User1997074450 posted
In Web Forms: VS MVC
How to read the parameters in URL of MVC?
In WebForms, I can say request.querystring["name"]
Thursday, May 27, 2021 1:21 PM
Answers
-
User753101303 posted
It can be even simpler with just :
public class HomeController : Controller { public ActionResult Index(string id, string name) { return View(); // put a breakpoint here } }
You should see first that id and name are null. Now if you try again with http://localhost:xxxxx/Home/Index/10?name=test in the browser address bar you should see that id is 10 and name is "test".
Edit: if using ASP.NET Core see perhaps https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-controller?view=aspnetcore-5.0&tabs=visual-studio
- Marked as answer by An0nym0u5User Tuesday, September 21, 2021 12:00 AM
Thursday, May 27, 2021 5:36 PM -
User287926715 posted
Hi vj78,
You can use the ViewBag method, the effect is as follows:
Controller:
public ActionResult Demo(string id, string name) { ViewBag.ID = id; ViewBag.name = name; return View(); }
View:
@{ ViewBag.Title = "Demo"; } <h2>Demo</h2> <div>ID: @ViewBag.ID</div> <div> Name:@ViewBag.name</div>
Result:
Best Regards,
ChaoDeng
- Marked as answer by An0nym0u5User Tuesday, September 21, 2021 12:00 AM
Friday, May 28, 2021 7:36 AM
All replies
-
User753101303 posted
Hi,
You are using ASP.NET 4.x or ASP.NET Core. Though it is ASP.NET and you can still Request.QueryStrng["name"] the basic idea is that http queries sent to the server are mapped to a controller class and an action method. Values sent to the server (form fields or querystring) are used to populate parameters. For example you'll write an action such as :
public ActionResult MyAction(string id,string name)
When using site.com/Home/MyAction/test?name=10, the usual default route will cause to locate the HomeController class, to call the MyAction method. id will be populated with "test" and name wth "10".
If using ASP.NET 4.x see for example https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/
Thursday, May 27, 2021 1:48 PM -
User475983607 posted
vj78
In Web Forms: VS MVC
How to read the parameters in URL of MVC?
In WebForms, I can say request.querystring["name"]
Honestly, this type of information is covered in every beginning level MVC tutorial. MVC has a feature called model binding that automatically binds URL and POST data to Action method input parameters. The binding is automatically so you do not have to write request.querystring["name"] to assign a variable. The model binder also handles validation so you don't have to write the same Web Forms validation over and over on every page.
The other part to model binding is called route parameters or routing. The URL can contain variables like an Id.
I strongly recommend going through the Getting Started tutorials and forget about how Web Forms works. Consider learning .NET 5 which is the latest framework.
ASP.NET MVC (classic)
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started
Working with Data
The .NET 5 MVC (latest)
Working with data
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-5.0
Thursday, May 27, 2021 2:38 PM -
User1997074450 posted
If I do this is this MVC way?
name = (string)RouteData.Values["name"];
Thursday, May 27, 2021 4:59 PM -
User753101303 posted
It can be even simpler with just :
public class HomeController : Controller { public ActionResult Index(string id, string name) { return View(); // put a breakpoint here } }
You should see first that id and name are null. Now if you try again with http://localhost:xxxxx/Home/Index/10?name=test in the browser address bar you should see that id is 10 and name is "test".
Edit: if using ASP.NET Core see perhaps https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-controller?view=aspnetcore-5.0&tabs=visual-studio
- Marked as answer by An0nym0u5User Tuesday, September 21, 2021 12:00 AM
Thursday, May 27, 2021 5:36 PM -
User1997074450 posted
How to get it in View?
Thursday, May 27, 2021 5:42 PM -
User475983607 posted
vj78
How to get it in View?The Controller Action passes a model to the View. This is a fundamental concept in MVC. Going through the tutorials will really help with your understanding.
Thursday, May 27, 2021 6:31 PM -
User287926715 posted
Hi vj78,
You can use the ViewBag method, the effect is as follows:
Controller:
public ActionResult Demo(string id, string name) { ViewBag.ID = id; ViewBag.name = name; return View(); }
View:
@{ ViewBag.Title = "Demo"; } <h2>Demo</h2> <div>ID: @ViewBag.ID</div> <div> Name:@ViewBag.name</div>
Result:
Best Regards,
ChaoDeng
- Marked as answer by An0nym0u5User Tuesday, September 21, 2021 12:00 AM
Friday, May 28, 2021 7:36 AM