This api contoller hit only when first two parameter is non nullable, if both datetime parameter is null,then it is not hitting ,I getting a 404 error , please give a solution
[HttpGet("userReports/{empid:int}/{sdate:DateTime?}/{edate:DateTime?}")]
public IEnumerable<object> usersReport(int empid,DateTime? sDate, DateTime? edate)
{
return employeeDailyStatusService.GetUserReport(empid, sDate, edate);
}
I'm using this url for testing in postman
https://localhost:44308/api/EmployeeDailyStatus/userReports/14/null/null
Any routing will translate you url
https://localhost:44308/api/EmployeeDailyStatus/userReports/14/null/null
as this
https://localhost:44308/api/EmployeeDailyStatus/userReports/14/"null"/"null"
null will be just a string "null".
You just need this url:
https://localhost:44308/api/EmployeeDailyStatus/userReports/14
Related
I have written the following HttpGet method and calling it with 2 parameters. When I try invoking it via postman , I get 404
not found error. Not sure what the problem is in my call
[HttpGet]
[AllowAnonymous]
[Route("unique-email/{clientCompanyId:int}/{email}")]
public IActionResult UniqueEmail( int clientCompanyId, string email )
{
_identityService.CheckUniqueEmail(clientCompanyId, email );
return Ok();
}
I tried the following ways to invoke it
http://localhost:57973/unique-email?clientCompanyId=29&email=test#test.co.uk
http://localhost:57973/unique-email?clientCompanyId=29&email="test#test.co.uk"
http://localhost:57973/unique-email?clientCompanyId=29&email='test#test.co.uk'
Code
public bool CheckUniqueEmail(int clientCompanyId, string email)
{
return _userUow.UniqueEmail(clientCompanyId, email);
}
public bool UniqueEmail(int clientCompanyId, string email)
{
bool anyMatching = ClientCompanyContactRepository.Get()
.Any(x => x.Email == email && x.ClientCompanyId == clientCompanyId);
return !anyMatching;
}
First, check if you have a Route attribute on controller or set any url prefix like api somewhere.
Your urls doesn't meets the requirement path
/unique-email?clientCompanyId=29&email=test#test.co.uk
because route attribute is
[Route("unique-email/{clientCompanyId:int}/{email}")]
it means that url should be
/unique-email/29/test#test.co.uk
If you need to pass parameters as a query string then use [FromQuery] attribute
The route you use in the controller is incorrect with the route you specify in the postman.
your Route
[Route("unique-email/{clientCompanyId:int}/{email}")]
And Uses
http://localhost:57973/unique-email?clientCompanyId=29&email=test#test.co.uk
http://localhost:57973/unique-email?clientCompanyId=29&email="test#test.co.uk"
http://localhost:57973/unique-email?clientCompanyId=29&email='test#test.co.uk'
With this route you used in the code you can use it in the postman
http://localhost:57973/unique-email/29/test#test.co.uk
But if you want to route your route as you did in the postman you first built, make a change to your code.
[HttpGet]
[AllowAnonymous]
[Route("unique-email")]
public IActionResult UniqueEmail([FromQuery] int clientCompanyId, [FromQuery] string email )
{
_identityService.CheckUniqueEmail(clientCompanyId, email );
return Ok();
}
And Postman
http://localhost:57973/unique-email?clientCompanyId=29&email="test#test.co.uk"
i have such redirect within my javascript:
window.location.href = '/webapp/record.action?date='+varDate+'&id=' +
varId;
When i execute this, i invoke my spring-handler as expected:
public void record(Model model, #RequestParam(value="varDate") String date, #RequestParam(value="varId",) String id){...}
But my second parameter "varId" is everytime null. When i'am looking on my HttpServletRequest i see instead of shown url this url:
/webapp/record.action?varDate=2017-07-01&_=1500358872039)#1495183143
org.eclipse.jetty.server.Request#591eaf27
How this url has been created? Why i lost second parameter "varId" ?
you are passing the parameter as id and in spring controller you are trying to retrieve it as varId.
change you code in controller as below:
public void record(Model model, #RequestParam(value="varDate") String date, #RequestParam(value="id",) String id){
// your custom logic
}
Hi every one I am new to Asp.net Web API and I had my question post data using asp.net Web API and got my answer accepted.
This is an extension to the same question I want to post data with some header value in the Postman and my code is as follows
public HttpResponseMessage PostCustomer([FromBody] NewUser userData, string devideId)
{
//My code
return response;
}
When I hit this in Postman passing values in JSON format in BODY - raw I got message as follows
No HTTP resource was found that matches the request URI
No action was found on the controller that matches the request.
Please help me.
It looks like you have added some additional devideId string parameter to your action. Make sure that you are supplying a value to it as a query string when making the request:
POST http://localhost:58626/api/customers?devideId=foo_bar
If you don't want to make this parameter required then you should make it optional (in terms of optional method parameter in .NET):
public HttpResponseMessage PostCustomer([FromBody] NewUser userData, string devideId = null)
{
...
}
Now you can POST to http://localhost:58626/api/customers without providing a value for this parameter.
Remark: You don't need to decorate a complex object type (such as NewUser) with the [FromBody] attribute. That's the default behavior in Web API.
UPDATE: Here's how you could read a custom header:
public HttpResponseMessage PostCustomer(NewUser userData)
{
IEnumerable<string> values;
if (this.Request.Headers.TryGetValues("X-MyHeader", out values))
{
string headerValue = values.FirstOrDefault();
}
...
return response;
}
I'm having trouble getting the Web API 2 attribute routing to work.
I've been trying everything I could find this whole evening but I can't find the problem.
What I want to achieve is the following:
Make a POST request to http://localhost:xxxx/api/chat/joingroup/1234 to get to the following API call:
[Route("joingroup/{id}")]
[HttpPost]
public async Task<IHttpActionResult> JoinGroup(string id, string connectionID)
{
await hubContext.Groups.Add(connectionID, id);
return Ok(hubContext.Groups.ToString());
}
This keeps getting me a http 400 message.
{"message":"No HTTP resource was found that matches the request URI 'http://localhost:41021/api/chat/joingroup/123'.",
"messageDetail":"No action was found on the controller 'Chat' that matches the request."}
But sending a post to: http://localhost:41021/api/chat/sendmessage/pm/123123 and also to http://localhost:41021/api/chat/joingroup gives me a 200
The chatcontroller:
[RoutePrefix("api/chat")]
public class ChatController : ApiController
{
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
[...]
[Route("joingroup/{id}")]
[HttpPost]
public async Task<IHttpActionResult> JoinGroup(string id, string connectionID)
{
await hubContext.Groups.Add(connectionID, id);
return Ok(hubContext.Groups.ToString());
}
HTTP POSTS to http://localhost:xxxx/api/chat/sendmessage are working fine.
I cannot figure out why it isn't going to the correct method when I'm calling a POST on http://localhost:xxxx/api/chat/joingroup/1234.
SOLUTION:
The solution was to reference both values that are needed in the JoinGroup method, id and connectionID. Now the request will hit this method.
Using:
http://localhost:xxxx/api/chat/joingroup/john?connectionid=123 will work.
I noticed two things on the code you sent through:
the path you POST to is: localhost:xxxx/joingroup/1234 , this
should be localhost:xxxx/api/chat/joingroup/1234
because you have 2 parameters for the joingroup, you will need to pass both of them through, may be like this localhost:xxxx/api/chat/joingroup/1234?connectionID=value or you can pass it on the request body
if the connectionID is optional you can modify the method to use option al parameters like this
public string JoinGroup(string id, string connectionID = "")
please let me know if this helps.
Thanks
Ashraf
I assume the connectionID parameter references the POSTed data. The easiest thing to make it work is to decorate it with the [FromBody] attribute and put an = in front of the value being sent like this: =MyConnection1.
Web API expects an object with properties or an array otherwise. Alternatively, you can wrap the connection ID with a custom class and pass it serialized as JSON/XML.
I'm trying to pass in an URL as a string parameter to a WEB API GET method.
The controller:
public class LinksController : ApiController
{
public HttpResponseMessage Get(string targetUrl)
{
//query db with targetURL
}
}
The aim is to query the database to see if the URL is stored. This works fine with simple URLs and URLs whose query string contains a single parameter, like:
http://www.youtube.com/watch?v=nLPE4vhSBx4
The problem I'm encountering is specifically when the query string contains multiple parameters, e.g.
http://www.youtube.com/watch?v=nLPE4vhSBx4&feature=youtube_gdata
When debugging, the value of targetUrl is only ".../watch?v=nLPE4vhSBx4" which means &feature=youtube_gdata is lost.
The GET request looks like this:
http://localhost:58056/api/links?targetUrl=http://www.youtube.com/watch? v=nLPE4vhSBx4&feature=youtube_gdata
I've also tried to add the following route in WebApiConfig.cs:
config.Routes.MapHttpRoute(
name: "Links",
routeTemplate: "api/links/{targetUrl}",
defaults: new { controller = "Links", targetUrl= RouteParameter.Optional }
);
But the GET request then results in 400 Bad Request.
So my question is, can't this be done? I would like the complete URL! Or would I need to change the method header to use the [FromBody] attribute and pass it as a JSON object?
You should URLEncode your target URL parameter so that it doesn't get mistaken for subsequent query string parameter. This means the URL you specified should appear as:
http://localhost:58056/api/links?targetUrl=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DnLPE4vhSBx4%26feature%3Dyoutube_gdata
And then inside your Get method, URLDecode the string that is passed as a parameter.
Both methods can be found in System.Web.HttpUtility