【发布时间】:2015-02-28 12:29:21
【问题描述】:
我发生了一些我不理解的奇怪行为。我的表的主键是一个字符串,因此我在 mvc 操作中使用字符串作为我的 id。
我的网址如下所示:
http://localhost:3333/profile/edit/fsdfsdsdfsdfff
动作如下所示:
public ActionResult Edit(string id)
{
return View(id);
}
我检查并确保 id 变量不为空。它已正确填充。
我的路线配置如下所示:
routes.MapRoute(
name: "Default2",
url: "Profile/Edit/{id}",
defaults: new { controller = "Profile", action = "Edit" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
这是错误:
未找到视图“fsdfsdsdfsdfff”或其主视图或没有视图 引擎支持搜索的位置。以下位置是 搜索:~/Views/Profile/fsdfsdsdfsdfff.cshtml ~/Views/Profile/fsdfsdsdfsdfff.vbhtml ~/Views/Shared/fsdfsdsdfsdfff.cshtml ~/Views/Shared/fsdfsdsdfsdfff.vbhtml
为什么要使用我的路线值来查找视图?可能值得注意的是,操作中的 id 值是一个字符串。如果我将其更改为 int,则可以正常工作。什么给了?
【问题讨论】:
-
看起来您一开始就不需要自定义路由,您的默认路由将满足您的目的。
-
你会这么认为,但我首先添加了自定义路由来解决问题。
-
我仍然坚持我的观点,你不需要那个自定义路线。
标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing