Reputation: 119
I applied Attribute routing to my RouteConfig.cs and added [Route("Store")] attribute on Action but getting error when access the url.
Working URL is http://localhost:52859/shop/store/dominos
I want it to change to http://localhost:52859/store/dominos
But post updating Attribute routing I see error on page as
Server Error in '/' Application. Runtime Error Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.
and in URL it shows as
http://localhost:52859/Errors/Error404?aspxerrorpath=/shop/store/dominos
Updated in RouteConfig
routes.MapMvcAttributeRoutes();
Action to call Stores
// GET: /shop/category/name
[Route("Store")]
public ActionResult Store(string name)
{
// Declare a list of Coupons
List<Coupn> coupnList;
using (ApplicationDbContext db = new ApplicationDbContext())
{
// Get store id
Store storeDTO = db.Store.Where(x => x.Slug == name).FirstOrDefault();
int storeId = storeDTO.StoreID;
ViewBag.TopDesc = storeDTO.TopDesc;
ViewBag.MainDesc = storeDTO.MainDesc;
ViewBag.StoreLogo = storeDTO.StoreLogo;
ViewBag.StoreName = storeDTO.StoreName;
// Init the list
coupnList = db.Coupns.ToArray().Where(x => x.StoreID == storeId).ToList();
// Get Store name
var coupnStore = db.Coupns.Where(x => x.StoreID == storeId).FirstOrDefault();
ViewBag.StoreName = coupnStore.StoreName;
}
// Return view with list
return View(coupnList);
}
Help on this is much appreciated.
Upvotes: 1
Views: 2802
Reputation: 874
You need to change the route at the Controller
level for the URL you want to use to work and set the action
route to "{name}"
. Like this:
[Route("Store")]
public class StoreController: Controller
{
// GET: /store/name
[Route("{name}")]
public ActionResult Store(string name)
{
// Declare a list of Coupons
List<Coupn> coupnList;
using (ApplicationDbContext db = new ApplicationDbContext())
{
// Get store id
Store storeDTO = db.Store.Where(x => x.Slug == name).FirstOrDefault();
int storeId = storeDTO.StoreID;
ViewBag.TopDesc = storeDTO.TopDesc;
ViewBag.MainDesc = storeDTO.MainDesc;
ViewBag.StoreLogo = storeDTO.StoreLogo;
ViewBag.StoreName = storeDTO.StoreName;
// Init the list
coupnList = db.Coupns.ToArray().Where(x => x.StoreID == storeId).ToList();
// Get Store name
var coupnStore = db.Coupns.Where(x => x.StoreID == storeId).FirstOrDefault();
ViewBag.StoreName = coupnStore.StoreName;
}
// Return view with list
return View(coupnList);
}
}
If you are working on ASP.NET MVC
make sure to activate Routing:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: “Default”,
url: “{controller}/{action}/{id}”,
defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
);
}
If you are working on .Net Core
:
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env
)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
Upvotes: 2
Reputation: 12619
Update your Route
like [Route("~/[action]/{name}")]
.
Check this to get more details about Routing https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#attribute-routing-for-rest-apis
Your action
will look like below.
[Route("~/[action]/{name}")]
public ActionResult Store(string name)
{
// Your code
}
Upvotes: 2