2

I am new in ASP.NET MVC. One of my first task I would like to do is passing a parameter to the HomeController.

To accomplish this I modified default Index method to:

public string Index(string strParam) { return "Param: " + strParam; } 

I also pass my parameter via url as:

https://localhost:44308/home/index/test 

but the parameter strParam is always null. How can I pass some parameters to my method using url?

My route config is default:

public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } 

5 Answers 5

4

You should name the parameter in the Index method id, as per the route definition. Otherwise, if you don't want to change it, add an HttpGet attribute such as:

[HttpGet] [Route("[action]/{strParam}") 
Sign up to request clarification or add additional context in comments.

6 Comments

It was a good idea to change the name of parameter in the method and it works but as u said it is possible to reach goal in other way. Where can I add 'HttpGet' attribute? Because When I am trying to add it above my method I got an error "HttpGetAttribute' does not contain a constructor that takes 1 arguments". My project is .Net Framework.
Ah, I thought it was ASP.NET Core! I updated the response!
As you suggested I modified method and now it looks like [HttpGet] [Route("home/index/{strParam}")] public string Index(string strParam) but unfortunately it still doesnt work.
Can you try, just for debugging, to comment out the route definition in RegisterRoutes?
When I comment out the route definition I just have "HTTP Error 404.0 - Not Found" and nothing works.
|
2

As alternative, in your route its defined as id. If you name your paramater as such, it will also work.

public string Index(string id) 

Comments

0

try to pass it using query string for eg. https://localhost:44308/home/index?strParam=test.

sharing you snap please have a look enter image description here

Comments

0

then try the following:

 [Route("[controller]/[action]")] [ApiController] public class TestController : Controller { [HttpGet("{value}")] [Route("/Test/Index")] public string Index(string value) { return "Hello " + value; } 

in my startup -> Configure goes below:

app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default1", pattern: "{controller}/{action}"); }); 

http://localhost:5001/test/index/World

Output: Hello World

Comments

-1

have you try the following:

 [HttpGet("{strParam}")] [Route("/Home/Index")] public string Index(string strParam) 

3 Comments

I think it a properly solution but I have an error "HttpGetAttribute' does not contain a constructor that takes 1 arguments".
check your map route
@Don2: did any of this helped you?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.