gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 属性路由

转载 作者:行者123 更新时间:2023-12-02 03:37:20 24 4
gpt4 key购买 nike

我决定使用属性路由而不是旧的方式。现在我面临一个问题:

这是我的 RouteConfig:

public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.LowercaseUrls = true;

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapMvcAttributeRoutes();
}
}

这是我的 HomeController:

public class HomeController : Controller
{
// some database stuff

[Route("{page?}")]
public ActionResult Index(int? page)
{
int pageNumber = page ?? 1;
int pageCount = 1;
return View(db.SelectPaged(pageNumber, pageCount));
}

[Route("about")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";

return View();
}
}

这是 ArticleController:

[RoutePrefix("articles")]
public class ArticlesController : Controller
{
private ClearDBEntities db = new ClearDBEntities();

// GET: Articles
[Route("")]
public ActionResult Index()
{
var articles = db.Articles.Include(a => a.Admin);
return View(articles.ToList());
}

// GET: Articles/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Article article = db.Articles.Find(id);
if (article == null)
{
return HttpNotFound();
}
return View(article);
}

问题:

当我运行应用程序并浏览默认地址( http://localhost:57922 )时,一切正常。它显示了主 Controller 的索引操作,关于页面也可以正常工作,分页也可以正常工作。

但是当我浏览到( http://localhost:57922/article )时,它给了我:

Server Error in '/' Application.

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

The request has found the following matching controller types:
ClearBlog.Controllers.ArticlesController
ClearBlog.Controllers.HomeController

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

The request has found the following matching controller types:
ClearBlog.Controllers.ArticlesController
ClearBlog.Controllers.HomeController

我不明白当我明确表示我要浏览带有“文章”前缀的页面时,框架怎么会感到困惑。

我希望应用程序在浏览到/article 时显示索引 View 。至于主页,我希望它在 url 中没有提供其他参数时继续显示索引。 (就像它已经做的一样)

如何修复它?

最佳答案

您出现此错误是因为 http://localhost:57922/articles匹配很多路由,正好有两个 Action :

  • IndexArticlesController :articles用作匹配 ArticlesController 的 Controller 默认操作的名称等于 Index .
  • IndexHomeController :articles用作来自名为 HomeController 的默认 Controller 的页面参数.

要通过使用属性路由解决此问题,您必须在 HomeController 的 Index 操作中的页面参数中添加约束,如下所示:

[Route("{page:int?}")]
public ActionResult Index(int? page)
{
//....
}

这样做这条路线将不会匹配/articles 因为 articles将用作 string类型,并且与 HomeController 的 Index 中的约束不匹配。

关于asp.net-mvc - ASP.NET MVC 属性路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33533469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com