2016-06-23 9 views
-1

Hallo allerseits Ich möchte URL wie {Controller}/{aktion}/{postid} - {Adresse} bekommen, aber routerUrl gibt null zurück, bitte hilf mir, es zu lösen. (ich bin Neuling in MVC)Url.routeUrl gibt null zurück (ich habe die gleichen Fragen ausprobiert)

meine Route Config

routes.MapRoute(
      name: "Post", 
      url: "Posts/Show/{postid}-{address}", 
      defaults: new { controller = "Posts", action = "Index", postid = "", address = "" } 
      ); 

und index.cshtml

<a href="@Url.RouteUrl("Post",new {item.PostId,item.Address })">@item.PostTitle</a> 

die uRL, die zu erzeugen ist http://localhost:59066/Posts/Show/1-Post-with-Featured-Image

aber in Posts

public ActionResult Show(string add) 
    { return View();} 

"string add" ist null!

+0

Ihr Parameter 'add' passt nicht zu' {postid} 'oder' {address} '! –

Antwort

0

ich die Strecke

routes.MapRoute("Post", "post/{postid}-{address}", new { controller = "Posts", action = "Show" ,postid="",address=""}, namespaces); 

zu

geändert und eine Route mit dem gleichen Controller und die Aktion hinzugefügt

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); 

dann richtig Aktion empfangen "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 . 
0

Ich würde nicht die Routen ändern ...

try this ...

<a href="@Url.Action("Action","Controller",new {PostId = item.PostId, Address = item.Address })">@item.PostTitle</a> 

Dies wird postID und Adresse als Parameter senden, so dass Sie sie in der Steuerung wie bekommen:

public ActionResult AwesomeThings(int PostId, String Address) 
{ 
     var foo = PostId; 
     var bar = Address; 
     return View(model); 
} 
0

Keine Änderungen im Routing,

Index.cshtml:

<a href="@Url.RouteUrl("Post",new {@postid = item.PostId, @address = item.Address },null)">@item.PostTitle</a> 

Controller:

public ActionResult Show(string postid, string address) 
{ return View();}