Url.routeUrl returns null(我试过同样的问题)
Url.routeUrl returns null (I've tried same questions)
大家好我想url喜欢{Controller}/{action}/{postid}-{address}
但是 routUrl returns null 请帮我解决它。(我是 MVC 的新手)
我的路线配置是
routes.MapRoute(
name: "Post",
url: "Posts/Show/{postid}-{address}",
defaults: new { controller = "Posts", action = "Index", postid = "", address = "" }
);
和index.cshtml
<a href="@Url.RouteUrl("Post",new {item.PostId,item.Address })">@item.PostTitle</a>
生成的url是
http://localhost:59066/Posts/Show/1-Post-with-Featured-Image
但在 PostsController 中
public ActionResult Show(string add)
{ return View();}
"string add" 为空!
我不会改变路线...
试试这个...
<a href="@Url.Action("Action","Controller",new {PostId = item.PostId, Address = item.Address })">@item.PostTitle</a>
这会将 PostId 和地址作为参数发送,因此您可以像这样在控制器中获取它们:
public ActionResult AwesomeThings(int PostId, String Address)
{
var foo = PostId;
var bar = Address;
return View(model);
}
路由没有变化,
Index.cshtml:
<a href="@Url.RouteUrl("Post",new {@postid = item.PostId, @address = item.Address },null)">@item.PostTitle</a>
控制器:
public ActionResult Show(string postid, string address)
{ return View();}
我把路线改成了
routes.MapRoute("Post", "post/{postid}-{address}", new { controller = "Posts", action = "Show" ,postid="",address=""}, namespaces);
并添加了具有相同控制器和操作的路由
routes.MapRoute("PostAddress", "post/{IdAndAdd}", new { controller = "Posts", action = "Show" }, namespaces);
routes.MapRoute("Post", "post/{postid}-{address}", new { controller = "Posts", action = "Show" ,postid="",address=""}, namespaces);
然后 "idAndAdd" 正确接收到操作
public ActionResult Show(string idAndAdd)
{
var parts = SeperateAddress(idAndAdd);
if (parts == null)
return HttpNotFound();
var post = db.Posts.Find(parts.Item1);
if (post == null)
return HttpNotFound();
if (!post.Address.Equals(parts.Item2, StringComparison.CurrentCultureIgnoreCase))
return RedirectToRoutePermanent("Post", new { postid = parts.Item1, address = post.Address });
return View(post);
}
and it's worked .
大家好我想url喜欢{Controller}/{action}/{postid}-{address} 但是 routUrl returns null 请帮我解决它。(我是 MVC 的新手)
我的路线配置是
routes.MapRoute(
name: "Post",
url: "Posts/Show/{postid}-{address}",
defaults: new { controller = "Posts", action = "Index", postid = "", address = "" }
);
和index.cshtml
<a href="@Url.RouteUrl("Post",new {item.PostId,item.Address })">@item.PostTitle</a>
生成的url是 http://localhost:59066/Posts/Show/1-Post-with-Featured-Image
但在 PostsController 中
public ActionResult Show(string add)
{ return View();}
"string add" 为空!
我不会改变路线...
试试这个...
<a href="@Url.Action("Action","Controller",new {PostId = item.PostId, Address = item.Address })">@item.PostTitle</a>
这会将 PostId 和地址作为参数发送,因此您可以像这样在控制器中获取它们:
public ActionResult AwesomeThings(int PostId, String Address)
{
var foo = PostId;
var bar = Address;
return View(model);
}
路由没有变化,
Index.cshtml:
<a href="@Url.RouteUrl("Post",new {@postid = item.PostId, @address = item.Address },null)">@item.PostTitle</a>
控制器:
public ActionResult Show(string postid, string address)
{ return View();}
我把路线改成了
routes.MapRoute("Post", "post/{postid}-{address}", new { controller = "Posts", action = "Show" ,postid="",address=""}, namespaces);
并添加了具有相同控制器和操作的路由
routes.MapRoute("PostAddress", "post/{IdAndAdd}", new { controller = "Posts", action = "Show" }, namespaces);
routes.MapRoute("Post", "post/{postid}-{address}", new { controller = "Posts", action = "Show" ,postid="",address=""}, namespaces);
然后 "idAndAdd" 正确接收到操作
public ActionResult Show(string idAndAdd)
{
var parts = SeperateAddress(idAndAdd);
if (parts == null)
return HttpNotFound();
var post = db.Posts.Find(parts.Item1);
if (post == null)
return HttpNotFound();
if (!post.Address.Equals(parts.Item2, StringComparison.CurrentCultureIgnoreCase))
return RedirectToRoutePermanent("Post", new { postid = parts.Item1, address = post.Address });
return View(post);
}
and it's worked .