MVC5路由问题

编程入门 行业动态 更新时间:2024-10-26 20:30:40
本文介绍了MVC5路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的主页面上,我有一个文章项目列表,其中包含每个项目的链接,链接将我带到ArticleController,我可以根据我作为参数传递的postID查找文章。

On my main page I have a list of Article items with a link to each item, the link take me to an ArticleController where I can look up the Article based on the postID I have passed as a parameter.

@foreach (var item in Model.PostFeatureList) { @Html.ActionLink((string)@item.Title, "Index", "ArticleController"); @Html.Raw(@item.Teaser) }

生成的是什么;

What's produced is;

<a postid="101" href="/ArticleController">EEPROM Programmer 2 of 4 - Communications</a>

哪个在假设Index操作是默认操作,我的思路在本地机器上调试是正确的。 然后在ArticleController.Index我有;

Which in my mind is correct for debugging on my local machine, assuming that Index action is the default action. Then in ArticleController.Index I have;

[Route("postID-{id}")] public ActionResult Index(int? id) { Post post = null; if (id != null) post = PostService.GetPostById((int)id); return View(post); }

现在问题点击我得到的链接; HTTP 404.您要查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用。请查看以下网址并确保拼写正确。 请求的网址:/ ArticleController 在我的主叫视图中;

So now the question when I click on the link I get; HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /ArticleController In the calling view I have;

@using (Html.BeginForm())

我需要什么吗在BeginForm中告诉它我想要路由到哪里或者是否有其他地方我需要让路由器知道我想要这样做? 谢谢Y 'all

Do I need something in the BeginForm to tell it where I want to route to or is there some other place I need to let the router know that I want to do this? Thanks Y'all

推荐答案

答案结果非常简单。 The answer turned out to be very simple. @foreach (var item in Model.PostFeatureList) { @Html.ActionLink((string)@item.Title, "Index", "Article"); @Html.Raw(@item.Teaser) }

路由器在ActionLink的Controller参数上添加Controller,因此删除它工作的Controller部分。 学习......宝贝步骤。

The router appends "Controller" on the Controller parameter in ActionLink so by removing the "Controller" portion it worked. Learning...baby steps.

更多推荐

MVC5路由问题

本文发布于:2023-11-12 13:46:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!