我遇到了错误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+" }
);
}
代码如下:
controller EditRaport:
public ActionResult EditRaport()
{
var query = from dbs in db.Raport
select dbs;
return View(query as IEnumerable<Raport>);
}
控制器EditRecords:
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
事实上,我的身份证像是“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;
谢谢!
答案 0 :(得分:0)
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+" }
);
}
在RegisterRoutes中删除以下路由:
routes.MapRoute(
"Home",
"Edit/{productId}",
new { controller = "Home", action = "Edit" },
new { productId = @"\w+" }
);