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
28 Replies
Why I can't fetch it?
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
I don't get right what you are say?
The jquery attribute should match action controller?
I am say controller have attributes, like
And method have attribute too
Attributes like
[Route]
affect the route
for example, in this case GetWhatever()
will be available on /unga/bunga/69
, /unga/bunga/420
and so on
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 parameteri create some modification to know the error i used your explantion as some guide but i still get same problem
Show your code
i curently switch code to get request not post to expermint iff mit will work
Your controller class attributes?
when i go ro index action it goes successfully
Index action in the cart controller?
yes
And what attributes does this one have?
public IActionResult Index()
{
var cart = HttpContext.Session.GetObject<List<CartItem>>("Cart") ?? new List<CartItem>();
return View(cart);
}
Attributes
no attrbutes
just as i typed
And what path do you access it at?
Just
/
? /cart
?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}
this is program.cs
Since the controller doesn't have a base name set up... that's what the
HttpGet
should implycaptered
What does "captered" mean?
it working
Nice
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
Attributes decide the path
There was nothing in the attributes that was adding the
/cart
segmentit 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
Yep, that works as well to create a base path for all actions