简体   繁体   中英

How can I configure ASP.NET MVC routing in C#?

Got a question about routing in ASP.NET MVC.

I want to have nice URLs in my app and that's why I use routing like

context.MapRoute(
     name: "ManageProducts_Home",
     url: "Eshop/ManageProducts/{eshopId}",
     defaults: new { controller = "ManageProducts", action = "Home", AreaName = "Eshop" }
);

which gives me nice urls like

https://localhost:44381/Eshop/ManageProducts/22

which is great!

Then I have another route:

context.MapRoute(
     name: "ManageProducts_AddProduct",
     url: "Eshop/ManageProducts/AddProduct/{eshopId}/{topCategoryId}",
     defaults: new { controller = "ManageProducts", action = "AddProduct", AreaName =  "Eshop" }
);

Which produces this URL:

https://localhost:44381/Eshop/ManageProducts/AddProduct/22/1

Ok, so far so good.

But on the second page ( AddProduct ), I need to refresh some partial view via AJAX which I call like this

$("._SelectCategory").load("/Eshop/ManageProducts/_SelectCategory", { categoryId: categoryId });

There is of course an action _SelectCategory in the controller which takes a parameter categoryId .

The problem is, this call is caught by the first routing and fails with exception missing eshopId in action Home, meaning this call doesn't go to the default routing I have there

context.MapRoute(
     "Eshop_default",
     "Eshop/{controller}/{action}/{id}",
     new { action = "Index", id = UrlParameter.Optional }
);

I need some advice how to configure the routing in view AddProduct works the ajax call.

Thank you

use a Web API Empty Controller and give your routes there that is more efficient eg: in ManageProducts controller

[HttpGet]
[Route("api/Eshop/ManageProducts/{eshopId}")]

In your Startup.cs configure meathod add this: app.UseMvc();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM