C# 属性路由不工作,并在URL中引发404错误
我将属性routing应用于我的RouteConfig.cs,并在操作中添加了[Route(“Store”)]属性,但在访问url时出错 工作URL是 我想把它改成 但在更新属性路由后,我看到页面上的错误如下 “/”应用程序中出现服务器错误。 运行时错误 描述:处理您的请求时发生异常。此外,在执行第一个异常的自定义错误页时发生另一个异常。请求已被终止 在URL中,它显示为 在RouteConfig中更新C# 属性路由不工作,并在URL中引发404错误,c#,asp.net-mvc,C#,Asp.net Mvc,我将属性routing应用于我的RouteConfig.cs,并在操作中添加了[Route(“Store”)]属性,但在访问url时出错 工作URL是 我想把它改成 但在更新属性路由后,我看到页面上的错误如下 “/”应用程序中出现服务器错误。 运行时错误 描述:处理您的请求时发生异常。此外,在执行第一个异常的自定义错误页时发生另一个异常。请求已被终止 在URL中,它显示为 在RouteConfig中更新 routes.MapMvcAttributeRoutes(); 致电店铺的行动 // G
routes.MapMvcAttributeRoutes();
致电店铺的行动
// 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);
}
//获取:/shop/category/name
[路线(“商店”)]
公共ActionResult存储(字符串名称)
{
//申报优惠券清单
清单清单;
使用(ApplicationDbContext db=new ApplicationDbContext())
{
//获取店铺id
Store storeDTO=db.Store.Where(x=>x.Slug==name).FirstOrDefault();
int storeId=storeDTO.storeId;
ViewBag.TopDesc=存储到.TopDesc;
ViewBag.MainDesc=存储到.MainDesc;
ViewBag.StoreLogo=storeDTO.StoreLogo;
ViewBag.StoreName=storeDTO.StoreName;
//初始化列表
coupnList=db.Coupns.ToArray();
//获取商店名称
var coupnStore=db.Coupns.Where(x=>x.StoreID==StoreID.FirstOrDefault();
ViewBag.StoreName=coupnStore.StoreName;
}
//返回带有列表的视图
返回视图(coupnList);
}
非常感谢您的帮助。您需要在
控制器
级别更改要用于工作的URL的路由,并将操作
路由设置为“{name}”
。像这样:
[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);
}
}
如果您正在使用.Net Core
:
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env
)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
您需要在
控制器
级别更改要用于工作的URL的路由,并将操作
路由设置为“{name}”
。像这样:
[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);
}
}
如果您正在使用.Net Core
:
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env
)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
更新您的
路线
,如[路线(“~/[action]/{name}”)]
选中此项可获取有关路由的更多详细信息
您的操作将如下所示
[Route("~/[action]/{name}")]
public ActionResult Store(string name)
{
// Your code
}
更新您的路线
,如[路线(“~/[action]/{name}”)]
选中此项可获取有关路由的更多详细信息
您的操作将如下所示
[Route("~/[action]/{name}")]
public ActionResult Store(string name)
{
// Your code
}
感谢您的快速响应,我刚刚尝试过,但也遇到了类似的错误。您可能有一些配置问题,请检查我的更新。太棒了,感谢您使其易于理解。就像一个符咒:DGlad,这是有帮助的。快乐编码@Riyaz在控制器中使用Route
后,您需要将其应用于所有操作。在Karan指出的案例中,要使该路由正常工作,您需要应用[route(“Xyz/{name}”)]
谢谢您的快速响应,我刚刚尝试过,但也遇到了类似的错误..您可能有一些配置问题,请查看我的更新。令人惊讶的是,感谢您使其易于理解。就像一个符咒:DGlad,这是有帮助的。快乐编码@Riyaz在控制器中使用Route
后,您需要将其应用于所有操作。在Karan指出的案例中,为了使该路线起作用,您需要应用[route(“Xyz/{name}”)]
此方法很好谢谢Karan对此的澄清。这方法很好,谢谢Karan对此的澄清。它能正常工作。