I have this controller
[Route("api/controller/method/{a}/{b}")]
public string Get(string a,string b)
{
return a+"__"+b;
}
It works only when I send this type of request in browser
api/controller/method/a/b
How can I modify controller so I could send such type of request:
api/controller/method?a=a&b=b
As I understood from the example you showed, you want to use query format to pass the values. You can achieve this like this:
[HttpGet("api/controller/method")]
public string Get([FromQuery]string a, [FromQuery]string b)
{
return a+"__"+b;
}
Related
I have this controller method
//[HttpGet("{id}")]
public IActionResult Nav(string id)
{
return HtmlEncoder.Default.Encode($"Hello {id}");
//return Content("Here's the ContentResult message.");
}
that i want to pass a string parameter and display it when i visit the controller method https://localhost:7123/Home/Nav/Logan. I get this error.
Cannot implicitly convert type 'string' to 'Microsoft.AspNetCore.Mvc.IActionResult'
I am using asp net core 6.
It is throwing this error as you are returning a string when it expects an IActionResult. You can easily solve this by returning Ok($"Hello {id}");
Let me explain.
When you perform following action.
return HtmlEncoder.Default.Encode($"Hello {id}");
This will return string and your method expect IActionResult so it get failed.
Solution 1
public string Nav(string id)
{
return HtmlEncoder.Default.Encode($"Hello {id}");
}
Now if you two paramter then you have to configure route that way. Default route only expect one Id.
[HttpGet("Nav/{id}/{two}")]
public string Nav(string id,string two)
{
return HtmlEncoder.Default.Encode($"Hello {id},{two}");
}
Solution 2
You can use Content or Ok Result and provide your output.
public IActionResult Nav(string id)
{
return Ok(HtmlEncoder.Default.Encode($"Hello {id}"));
}
I fixed it this way. This is the url https://localhost:7123/Home/Nav/6?num=5&third=3
and this is the method
public IActionResult Nav(string id, int num, string third)
{
return Ok($"Hello {id} {num} {third}");
}
I try to trigger a specific method (the second one) in HeroesController class:
[HttpGet]
public IEnumerable<Hero> Get()
{
return Heroes;
}
[HttpGet("/name={term}")]
public IEnumerable<Hero> Get(string term)
{
return Heroes;
}
After calling this URL:
https://localhost:44375/heroes/?name=Spider
The first method is triggered, not the second. Why is that so? How to trigger the second one which receives term parameter?
As pointed out in the comments by King King, the url is not matched, but the best way to do this is;
[HttpGet]
public IEnumerable<Hero> Get([FromQuery] string term)
{
return Heroes;
}
Then the endpoint would be hit if a query parameter, term is passed https://localhost:44375/heroes?term=Spider
There are 2 things to distinguish here - URL parameters versus query parameters. If you want to supply variable while doing you GET HTTP call these are the options:
Variable you want to pass can be part of the URL:
http://localhost:8080/yourResourceName/{varValue}
[HttpGet]
public Task<IActionResult> Get(string varValue)
{
}
Variable you want to pass can be a query parameter:
http://localhost:8080/yourResourceName?varname={varValue}
[HttpGet]
public Task<IActionResult> Get([FromQuery]string varValue)
{
}
In asp.net app i receive a form values from angular app.
[HttpPost]
public IActionResult addChange([FromBody] Change change)
{
return Json(change.Status);
}
How to get object or some value of object to use it in another class?
You should be able to access property like that: change.PropertyName.
But you might send data from angular as FormData, then you should change [FromBody] to [FromForm].
It's most likely that you are doing something wrong at angular site. You should check this endpoint via postman.
Edit:
To use this object in another method you should pass it through.
[HttpPost]
public IActionResult addChange([FromBody] Change change)
{
AnotherMethod(change);
return Json(change.Status);
}
public void AnotherMethod(Change change)
{
var foo = change.Status;
}
I have two methods:
[HttpGet("{id}")]
public IActionResult GetTask([FromRoute] int id)
{
}
[HttpGet("{userId}")]
public IActionResult GetUserTask([FromRoute] string userId)
{
}
As you can see, i want to pass to my API routes like:
https://localhost:44365/Task/1
and
https://localhost:44365/Task/string
But my WebApi project cant handle it. When i pass route like this:
https://localhost:44365/Task/7dd2514618c4-4575b3b6f2e9731edd61
i get an 400 http and this response:
{
"id": [
"The value '7dd2514618c4-4575b3b6f2e9731edd61' is not valid."
]
}
While debugging, im not hitting any methods (when i pass string instead of int)
My question is, how to verload methods with one parameters with string or int? These methods do diffrent things
EDIT
When i pass something like:
https://localhost:44365/Task/dddd
I still get response with invalid id
You can define parameter type like [HttpGet("{id:int}")]. For more information refer below link.
https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-constraints
Your actions should be like below.
[HttpGet("{id:int}")]
public IActionResult GetTask([FromRoute] int id)
{
}
[HttpGet("{userId}")]
public IActionResult GetUserTask([FromRoute] string userId)
{
}
Use like this
[HttpGet("{id}")]
public IActionResult GetTask([FromRoute] int id)
{
}
[HttpGet("User/{userId}")]
public IActionResult GetUserTask([FromRoute] string userId)
{
}
and while calling api with guid/string use
https://localhost:44365/Task/User/7dd2514618c4-4575b3b6f2e9731edd61
I'm trying to get the parameters of specific attribute routed URL on ActionFilterAttribute. For instance I have an action like below:
[Route("/v1/test/{userId}/{udid}")]
public object GetNewObject(int userId, string udid)
And in action filter attribute the absolute url is coming something like "http://test.example.com/v1/test/1/123-asda-231-asd". However I want to parse these parameters as userId=1 and udid=... within a collection.
Is it possible?
Anyway I found the answer,
Within RouteData of ControllerContext we may able to retrieve the specified value.
actionContext.ControllerContext.RouteData.Values["udid"]
[Route("...")] is possible only in MVC 5.
I think you want to do something like this
[RoutePrefix("api/users")]
public class UsersController : ApiController
{
// GET api/users
[Route("")]
public IEnumerable<User> Get() { ... }
// GET api/user/5
[Route("{id:int}")]
public Book Get(int id) { ... }
// POST api/users
[Route("")]
public HttpResponseMessage Post(User book) { ... }
}
where each User contains your properties
public class User
{
int UserId{get;set;}
string Udid{get; set;}
}