Index.chtml页面本身我有这个链接
@Html.ActionLink("Edit","EditUserProfile",new {vchUserID = item.vchUserID})内部用户控制器本身
// GET: /User/ViewUserProfile/1 public ActionResult EditUserProfile(string userID = "abdul@kareems.com") { LIVE_USER objUserFind = db.LIVE_USER.Find(userID); if (objUserFind == null) { return HttpNotFound(); } return View(objUserFind); } // //POST: /Admin/EditAdminProfile/1 [HttpPost] public ActionResult EditUserProfile(LIVE_USER objUserFind) { if (ModelState.IsValid) { db.Entry(objUserFind).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(objUserFind); }RouteConfig.cs文件本身我有像bellow的url结构 。
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{vchUserID}", defaults: new { controller = "Admin", action = "Index", vchUserID = UrlParameter.Optional /*kareem removed vchUserID = UrlParameter.Optional*/ } );我得到的结果是: * HTTP错误404.0 - 未找到您要查找的资源已被删除,名称已更改或暂时不可用。 *
Index.chtml page itself i have this link
@Html.ActionLink("Edit","EditUserProfile",new {vchUserID = item.vchUserID})Inside user Controller itself
// GET: /User/ViewUserProfile/1 public ActionResult EditUserProfile(string userID = "abdul@kareems.com") { LIVE_USER objUserFind = db.LIVE_USER.Find(userID); if (objUserFind == null) { return HttpNotFound(); } return View(objUserFind); } // //POST: /Admin/EditAdminProfile/1 [HttpPost] public ActionResult EditUserProfile(LIVE_USER objUserFind) { if (ModelState.IsValid) { db.Entry(objUserFind).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(objUserFind); }RouteConfig.cs file itself i have url structure like bellow.
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{vchUserID}", defaults: new { controller = "Admin", action = "Index", vchUserID = UrlParameter.Optional /*kareem removed vchUserID = UrlParameter.Optional*/ } );Result i got is: *HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.*
最满意答案
控制器方法中参数的名称需要与路径中定义的参数名称匹配。 目前,该参数在路由中名为vchUserID ,但在您的控制器方法中,您已将其命名为userId 。
这意味着控制器方法永远不会从Url获取值,并且您将始终获得默认值“abdul@hibrise.com”。 (我想这不是你的一个在线用户,所以你要返回HttpNotFound结果)
尝试在控制器方法中重命名参数,如下所示:
public ActionResult EditUserProfile(string vchUserID = "abdul@hibrise.com") { ... }The name of the parameter in your controller method needs to match the name of the parameter defined in your route. Currently the parameter is named vchUserID in the route but in your controller method you have named it userId.
That means the controller method will never get the value from the Url, and you will always get the default value "abdul@hibrise.com". (I guess that is not one of your live users so you are returning the HttpNotFound result)
Try renaming the parameter in the controller method as in:
public ActionResult EditUserProfile(string vchUserID = "abdul@hibrise.com") { ... }更多推荐
发布评论