ASP .net core HttpGet with parameter resulting Bad Request
So I have a service using Axios to call my C# API
since i wanto to select specific data, I use get method with parameter
here's my service
let response = await Axios.get('/api/get-report', {
params: filter
});
and here's my filter object in typescript
export interface FilterModel {
employeeId?: string;
Month?: Date;
from?: Date;
to?: Date;
}
model on server
public class AttendanceReportFilterModel
{
public string EmployeeId { set; get; }
public DateTime? Month { set; get; }
public DateTime? From { set; get; }
public DateTime? To { set; get; }
}
and finally, here's my C# API
[HttpGet("get-report")]
public async Task<IActionResult> GetReport(FilterModel filter)
{
var Detail = await Service.GetReport(filter);
if (Detail == null)
{
return StatusCode(500, "Not Found");
}
return Ok(Detail);
}
whenever I call my service, its always returning Bad Request
anybody know why? and how to fix this?
typescript api asp.net-web-api asp.net-core
add a comment |
So I have a service using Axios to call my C# API
since i wanto to select specific data, I use get method with parameter
here's my service
let response = await Axios.get('/api/get-report', {
params: filter
});
and here's my filter object in typescript
export interface FilterModel {
employeeId?: string;
Month?: Date;
from?: Date;
to?: Date;
}
model on server
public class AttendanceReportFilterModel
{
public string EmployeeId { set; get; }
public DateTime? Month { set; get; }
public DateTime? From { set; get; }
public DateTime? To { set; get; }
}
and finally, here's my C# API
[HttpGet("get-report")]
public async Task<IActionResult> GetReport(FilterModel filter)
{
var Detail = await Service.GetReport(filter);
if (Detail == null)
{
return StatusCode(500, "Not Found");
}
return Ok(Detail);
}
whenever I call my service, its always returning Bad Request
anybody know why? and how to fix this?
typescript api asp.net-web-api asp.net-core
When you are making request you are adding some parameters. Can you please provide URL from network? It seems that you are sending invalid date.
– Volodymyr Bilyachat
Nov 21 at 3:14
ShowFilterModel
as defined on server side. Also show thefilter
params sent from client. You most likely have the[ApiController]
and the filter is defaulting to bad request because of a missing or malformed parameter value.
– Nkosi
Nov 21 at 3:19
Does it get hit while you debug or it doesn't even find the Url ? Whereas am not sure how you pass a body for get ?
– Rahul
Nov 21 at 3:20
@Nkosi you can see my model in my post aboce, i aleady updated it. also i already use [ApiController]
– hphp
Nov 21 at 3:38
add a comment |
So I have a service using Axios to call my C# API
since i wanto to select specific data, I use get method with parameter
here's my service
let response = await Axios.get('/api/get-report', {
params: filter
});
and here's my filter object in typescript
export interface FilterModel {
employeeId?: string;
Month?: Date;
from?: Date;
to?: Date;
}
model on server
public class AttendanceReportFilterModel
{
public string EmployeeId { set; get; }
public DateTime? Month { set; get; }
public DateTime? From { set; get; }
public DateTime? To { set; get; }
}
and finally, here's my C# API
[HttpGet("get-report")]
public async Task<IActionResult> GetReport(FilterModel filter)
{
var Detail = await Service.GetReport(filter);
if (Detail == null)
{
return StatusCode(500, "Not Found");
}
return Ok(Detail);
}
whenever I call my service, its always returning Bad Request
anybody know why? and how to fix this?
typescript api asp.net-web-api asp.net-core
So I have a service using Axios to call my C# API
since i wanto to select specific data, I use get method with parameter
here's my service
let response = await Axios.get('/api/get-report', {
params: filter
});
and here's my filter object in typescript
export interface FilterModel {
employeeId?: string;
Month?: Date;
from?: Date;
to?: Date;
}
model on server
public class AttendanceReportFilterModel
{
public string EmployeeId { set; get; }
public DateTime? Month { set; get; }
public DateTime? From { set; get; }
public DateTime? To { set; get; }
}
and finally, here's my C# API
[HttpGet("get-report")]
public async Task<IActionResult> GetReport(FilterModel filter)
{
var Detail = await Service.GetReport(filter);
if (Detail == null)
{
return StatusCode(500, "Not Found");
}
return Ok(Detail);
}
whenever I call my service, its always returning Bad Request
anybody know why? and how to fix this?
typescript api asp.net-web-api asp.net-core
typescript api asp.net-web-api asp.net-core
edited Nov 21 at 3:37
asked Nov 21 at 3:00
hphp
701112
701112
When you are making request you are adding some parameters. Can you please provide URL from network? It seems that you are sending invalid date.
– Volodymyr Bilyachat
Nov 21 at 3:14
ShowFilterModel
as defined on server side. Also show thefilter
params sent from client. You most likely have the[ApiController]
and the filter is defaulting to bad request because of a missing or malformed parameter value.
– Nkosi
Nov 21 at 3:19
Does it get hit while you debug or it doesn't even find the Url ? Whereas am not sure how you pass a body for get ?
– Rahul
Nov 21 at 3:20
@Nkosi you can see my model in my post aboce, i aleady updated it. also i already use [ApiController]
– hphp
Nov 21 at 3:38
add a comment |
When you are making request you are adding some parameters. Can you please provide URL from network? It seems that you are sending invalid date.
– Volodymyr Bilyachat
Nov 21 at 3:14
ShowFilterModel
as defined on server side. Also show thefilter
params sent from client. You most likely have the[ApiController]
and the filter is defaulting to bad request because of a missing or malformed parameter value.
– Nkosi
Nov 21 at 3:19
Does it get hit while you debug or it doesn't even find the Url ? Whereas am not sure how you pass a body for get ?
– Rahul
Nov 21 at 3:20
@Nkosi you can see my model in my post aboce, i aleady updated it. also i already use [ApiController]
– hphp
Nov 21 at 3:38
When you are making request you are adding some parameters. Can you please provide URL from network? It seems that you are sending invalid date.
– Volodymyr Bilyachat
Nov 21 at 3:14
When you are making request you are adding some parameters. Can you please provide URL from network? It seems that you are sending invalid date.
– Volodymyr Bilyachat
Nov 21 at 3:14
Show
FilterModel
as defined on server side. Also show the filter
params sent from client. You most likely have the [ApiController]
and the filter is defaulting to bad request because of a missing or malformed parameter value.– Nkosi
Nov 21 at 3:19
Show
FilterModel
as defined on server side. Also show the filter
params sent from client. You most likely have the [ApiController]
and the filter is defaulting to bad request because of a missing or malformed parameter value.– Nkosi
Nov 21 at 3:19
Does it get hit while you debug or it doesn't even find the Url ? Whereas am not sure how you pass a body for get ?
– Rahul
Nov 21 at 3:20
Does it get hit while you debug or it doesn't even find the Url ? Whereas am not sure how you pass a body for get ?
– Rahul
Nov 21 at 3:20
@Nkosi you can see my model in my post aboce, i aleady updated it. also i already use [ApiController]
– hphp
Nov 21 at 3:38
@Nkosi you can see my model in my post aboce, i aleady updated it. also i already use [ApiController]
– hphp
Nov 21 at 3:38
add a comment |
1 Answer
1
active
oldest
votes
Try to add
[FromQuery]
public async Task<IActionResult> GetReport([FromQuery] FilterModel filter)
So since you are doing binding to object you need to say where to take them https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1#customize-model-binding-behavior-with-attributes.
Or you can do it just with parameters
public async Task<IActionResult> GetReport(string EmployeeId, DateTime? Month = null, DateTime? FromMonth = null, DateTime? ToMonth = null)
there is no FromUri only FromBody, Form, Header, Query, Route, Services which one i have to use?
– hphp
Nov 21 at 4:10
@hphp updated my answer. I did posted for old aspnet. In core there is fromQuery
– Volodymyr Bilyachat
Nov 21 at 4:13
well now it's getting error code 500
– hphp
Nov 21 at 4:15
@hphp well at least its not bad request :) Seems to be that endpoint is hit but error in your code
– Volodymyr Bilyachat
Nov 21 at 4:17
1
ah yeah.. it's hit.. thanks... now i just have to debug my code logic thanks
– hphp
Nov 21 at 4:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404685%2fasp-net-core-httpget-with-parameter-resulting-bad-request%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try to add
[FromQuery]
public async Task<IActionResult> GetReport([FromQuery] FilterModel filter)
So since you are doing binding to object you need to say where to take them https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1#customize-model-binding-behavior-with-attributes.
Or you can do it just with parameters
public async Task<IActionResult> GetReport(string EmployeeId, DateTime? Month = null, DateTime? FromMonth = null, DateTime? ToMonth = null)
there is no FromUri only FromBody, Form, Header, Query, Route, Services which one i have to use?
– hphp
Nov 21 at 4:10
@hphp updated my answer. I did posted for old aspnet. In core there is fromQuery
– Volodymyr Bilyachat
Nov 21 at 4:13
well now it's getting error code 500
– hphp
Nov 21 at 4:15
@hphp well at least its not bad request :) Seems to be that endpoint is hit but error in your code
– Volodymyr Bilyachat
Nov 21 at 4:17
1
ah yeah.. it's hit.. thanks... now i just have to debug my code logic thanks
– hphp
Nov 21 at 4:18
add a comment |
Try to add
[FromQuery]
public async Task<IActionResult> GetReport([FromQuery] FilterModel filter)
So since you are doing binding to object you need to say where to take them https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1#customize-model-binding-behavior-with-attributes.
Or you can do it just with parameters
public async Task<IActionResult> GetReport(string EmployeeId, DateTime? Month = null, DateTime? FromMonth = null, DateTime? ToMonth = null)
there is no FromUri only FromBody, Form, Header, Query, Route, Services which one i have to use?
– hphp
Nov 21 at 4:10
@hphp updated my answer. I did posted for old aspnet. In core there is fromQuery
– Volodymyr Bilyachat
Nov 21 at 4:13
well now it's getting error code 500
– hphp
Nov 21 at 4:15
@hphp well at least its not bad request :) Seems to be that endpoint is hit but error in your code
– Volodymyr Bilyachat
Nov 21 at 4:17
1
ah yeah.. it's hit.. thanks... now i just have to debug my code logic thanks
– hphp
Nov 21 at 4:18
add a comment |
Try to add
[FromQuery]
public async Task<IActionResult> GetReport([FromQuery] FilterModel filter)
So since you are doing binding to object you need to say where to take them https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1#customize-model-binding-behavior-with-attributes.
Or you can do it just with parameters
public async Task<IActionResult> GetReport(string EmployeeId, DateTime? Month = null, DateTime? FromMonth = null, DateTime? ToMonth = null)
Try to add
[FromQuery]
public async Task<IActionResult> GetReport([FromQuery] FilterModel filter)
So since you are doing binding to object you need to say where to take them https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1#customize-model-binding-behavior-with-attributes.
Or you can do it just with parameters
public async Task<IActionResult> GetReport(string EmployeeId, DateTime? Month = null, DateTime? FromMonth = null, DateTime? ToMonth = null)
edited Nov 21 at 4:12
answered Nov 21 at 3:55
Volodymyr Bilyachat
9,00222748
9,00222748
there is no FromUri only FromBody, Form, Header, Query, Route, Services which one i have to use?
– hphp
Nov 21 at 4:10
@hphp updated my answer. I did posted for old aspnet. In core there is fromQuery
– Volodymyr Bilyachat
Nov 21 at 4:13
well now it's getting error code 500
– hphp
Nov 21 at 4:15
@hphp well at least its not bad request :) Seems to be that endpoint is hit but error in your code
– Volodymyr Bilyachat
Nov 21 at 4:17
1
ah yeah.. it's hit.. thanks... now i just have to debug my code logic thanks
– hphp
Nov 21 at 4:18
add a comment |
there is no FromUri only FromBody, Form, Header, Query, Route, Services which one i have to use?
– hphp
Nov 21 at 4:10
@hphp updated my answer. I did posted for old aspnet. In core there is fromQuery
– Volodymyr Bilyachat
Nov 21 at 4:13
well now it's getting error code 500
– hphp
Nov 21 at 4:15
@hphp well at least its not bad request :) Seems to be that endpoint is hit but error in your code
– Volodymyr Bilyachat
Nov 21 at 4:17
1
ah yeah.. it's hit.. thanks... now i just have to debug my code logic thanks
– hphp
Nov 21 at 4:18
there is no FromUri only FromBody, Form, Header, Query, Route, Services which one i have to use?
– hphp
Nov 21 at 4:10
there is no FromUri only FromBody, Form, Header, Query, Route, Services which one i have to use?
– hphp
Nov 21 at 4:10
@hphp updated my answer. I did posted for old aspnet. In core there is fromQuery
– Volodymyr Bilyachat
Nov 21 at 4:13
@hphp updated my answer. I did posted for old aspnet. In core there is fromQuery
– Volodymyr Bilyachat
Nov 21 at 4:13
well now it's getting error code 500
– hphp
Nov 21 at 4:15
well now it's getting error code 500
– hphp
Nov 21 at 4:15
@hphp well at least its not bad request :) Seems to be that endpoint is hit but error in your code
– Volodymyr Bilyachat
Nov 21 at 4:17
@hphp well at least its not bad request :) Seems to be that endpoint is hit but error in your code
– Volodymyr Bilyachat
Nov 21 at 4:17
1
1
ah yeah.. it's hit.. thanks... now i just have to debug my code logic thanks
– hphp
Nov 21 at 4:18
ah yeah.. it's hit.. thanks... now i just have to debug my code logic thanks
– hphp
Nov 21 at 4:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404685%2fasp-net-core-httpget-with-parameter-resulting-bad-request%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When you are making request you are adding some parameters. Can you please provide URL from network? It seems that you are sending invalid date.
– Volodymyr Bilyachat
Nov 21 at 3:14
Show
FilterModel
as defined on server side. Also show thefilter
params sent from client. You most likely have the[ApiController]
and the filter is defaulting to bad request because of a missing or malformed parameter value.– Nkosi
Nov 21 at 3:19
Does it get hit while you debug or it doesn't even find the Url ? Whereas am not sure how you pass a body for get ?
– Rahul
Nov 21 at 3:20
@Nkosi you can see my model in my post aboce, i aleady updated it. also i already use [ApiController]
– hphp
Nov 21 at 3:38