MVC5路由问题 [英] MVC5 Routing question

查看:82
本文介绍了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路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆