Error in fetch action controller from jquery

I create simple add to cart feature and i have 3 image one for controller and and from jquery code and one for error in condole terminal
No description
No description
No description
28 Replies
Sharaf Abacery
Sharaf AbaceryOP2mo ago
Why I can't fetch it?
Angius
Angius2mo ago
Post all the attributes you have on the action method and on the controller class They are what controls what route the action will be available at
Sharaf Abacery
Sharaf AbaceryOP2mo ago
I don't get right what you are say? The jquery attribute should match action controller?
Angius
Angius2mo ago
I am say controller have attributes, like
[Attribute]
[AnotherAttribute]
public class SomethingController
{
[Attribute]
[AnotherAttribute]
public class SomethingController
{
And method have attribute too
[Attribute]
[AnotherAttribute]
public async Task<Whatever> GetWhatever()
{
[Attribute]
[AnotherAttribute]
public async Task<Whatever> GetWhatever()
{
Attributes like [Route] affect the route
[Route("unga")]
public class SomethingController
{
[HttpGet("bunga/{skunga}"]
public async Task<Whatever> GetWhatever(int skunga)
{

}
}
[Route("unga")]
public class SomethingController
{
[HttpGet("bunga/{skunga}"]
public async Task<Whatever> GetWhatever(int skunga)
{

}
}
for example, in this case GetWhatever() will be available on /unga/bunga/69, /unga/bunga/420 and so on
[Route("[controller]")]
public class SomethingController
{
[HttpGet("bunga"]
public async Task<Whatever> GetWhatever(int skunga)
{

}
}
[Route("[controller]")]
public class SomethingController
{
[HttpGet("bunga"]
public async Task<Whatever> GetWhatever(int skunga)
{

}
}
this one is special since it uses [controller], that will be replaced by the controller name. So the route will end up being /something/bunga?skunga=69, /something/bunga/skunga=420 and so on Since skunga is no longer a route parameter
Sharaf Abacery
Sharaf AbaceryOP2mo ago
i create some modification to know the error i used your explantion as some guide but i still get same problem
Angius
Angius2mo ago
Show your code
Sharaf Abacery
Sharaf AbaceryOP2mo ago
i curently switch code to get request not post to expermint iff mit will work
No description
No description
Angius
Angius2mo ago
Your controller class attributes?
Sharaf Abacery
Sharaf AbaceryOP2mo ago
when i go ro index action it goes successfully
No description
Angius
Angius2mo ago
Index action in the cart controller?
Sharaf Abacery
Sharaf AbaceryOP2mo ago
yes
Angius
Angius2mo ago
And what attributes does this one have?
Sharaf Abacery
Sharaf AbaceryOP2mo ago
public IActionResult Index() { var cart = HttpContext.Session.GetObject<List<CartItem>>("Cart") ?? new List<CartItem>(); return View(cart); }
Angius
Angius2mo ago
Attributes
Sharaf Abacery
Sharaf AbaceryOP2mo ago
no attrbutes just as i typed
Angius
Angius2mo ago
And what path do you access it at? Just /? /cart?
Angius
Angius2mo ago
Looks like some weird routing magic is at work, or you have it routed somewhere else, not with attributes In any case, try just /AddToCartAsync/{id}
Sharaf Abacery
Sharaf AbaceryOP2mo ago
this is program.cs
Angius
Angius2mo ago
Since the controller doesn't have a base name set up... that's what the HttpGet should imply
Sharaf Abacery
Sharaf AbaceryOP2mo ago
captered
Angius
Angius2mo ago
What does "captered" mean?
Sharaf Abacery
Sharaf AbaceryOP2mo ago
it working
Angius
Angius2mo ago
Nice
Sharaf Abacery
Sharaf AbaceryOP2mo ago
captured thanks alot for help me and i add this attrbute [HttpPost("/Cart/AddToCartAsync/{bookId}")] and it works but i still dont know why without attrbuites is not woking as i need
Angius
Angius2mo ago
Attributes decide the path There was nothing in the attributes that was adding the /cart segment
Sharaf Abacery
Sharaf AbaceryOP2mo ago
it could [HttpPost("/AddToCartAsync/{bookId}")] override the cart controller itself i could add attrbute to controller to enforce it [Route("Cart")] public class CartController : Controller [HttpPost("AddToCartAsync/{bookId}")] public async Task<IActionResult> AddToCartAsync(string bookId) @Angius thanks
Angius
Angius2mo ago
Yep, that works as well to create a base path for all actions

Did you find this page helpful?