【发布时间】:2017-01-21 00:40:25
【问题描述】:
我遇到了错误 404,这表明我缺少某些资源。
我真的对 ASP.NET MVC 没有经验,而路由是我最讨厌的地方,尽管这对我来说是意外错误,因为我有所需的控制器 + 操作。
1 Raport 有许多 ThrashType 记录。它们是单独的表,尽管我认为这并不重要。
问题是我得到了正确的 Raport 表视图和记录。
当我将链接悬停在视图中时,它的格式为 {host}/Raport/EditRecords/{string_id}
当我单击视图中的链接(试图转到 EditRecords)时,我收到 404 错误,这让 atm 感到困惑。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Home",
"Edit/{productId}",
new { controller = "Home", action = "Edit" },
new { productId = @"\w+" }
);
}
代码如下:
控制器EditRaport:
public ActionResult EditRaport()
{
var query = from dbs in db.Raport
select dbs;
return View(query as IEnumerable<Raport>);
}
控制器编辑记录:
public ActionResult EditRecords(string id)
{
var query = from dbs in db.ThrashType
where dbs.raport_id == id
select dbs;
return View(query as IEnumerable<ThrashType>);
}
EditRaport 视图:
@model IEnumerable<WebApplication1.Klasy_Raport.Raport>
@{
ViewBag.Title = "EditRaport";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Edit Raport</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserFrontInfo.userName)
</th>
<th>
@Html.DisplayNameFor(model => model.creation_time)
</th>
<th>
@Html.DisplayNameFor(model => model.last_modyfication)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserFrontInfo.userName)
</td>
<td>
@Html.DisplayFor(modelItem => item.creation_time)
</td>
<td>
@Html.DisplayFor(modelItem => item.last_modyfication)
</td>
<td>
@Html.ActionLink("Edit", "EditRecords", new { id=item.raport_id })
</td>
</tr>
}
</table>
您能告诉我错误可能在哪里吗?在这种情况下,我真的不明白 404。
编辑 15:37 13.09.2016
在@StephenMuecke 的帮助下,我了解了问题的原因。 他给了我一个链接,更详细地解释了这个问题: Dots in URL causes 404 with ASP.NET mvc and IIS
事实上,我的 ID 类似于“12.34.56 00:00:00abcde”。如果您尝试这样做,则会导致 IE 错误 (404)。
答案是将ID更改为“1234560000000abcde”之类的东西,这样IE就可以处理了。
在我的情况下,这完成了工作:
//remove dots and colons from string
var datePart = DateTime.Today.ToString().Replace(".", "").Replace(":","");
//giving string properties and assigning for alias
var namePart = User.Identity.Name.ToString();
//removing @blablabl.ab - 12 signs, 13 is for the sake of correct indexing
var nameShort = namePart.Remove((namePart.Length)-13, 12);
//final ID concatenation
newRaport.raport_id = datePart + nameShort;
谢谢!
【问题讨论】:
-
你对
EditRecords有意见吗? -
EditRecords 操作 - 它是 Raport 控制器的一部分吗?
-
那么你在控制器EditRecordsController中有一个动作EditRecords或者控制器的名字是什么?
-
不相关,但您的第二条路线毫无意义 - 除非将其放置在默认路线之前,否则它永远不会被击中(路线的顺序很重要)
-
这两种方法都在
RaportController中吗?
标签: c# asp.net asp.net-mvc routing