1

i dont understand the rooting in ASP.net ; im missing something with this ? this is my root :

 routes.MapRoute( name: "ChampionID", url: "Champion/ChampionById/id", defaults: new { controller = "Champion", action = "ChampionById", id = "5" } ); 

this is my Controler :

public class ChampionController : Controller { public ActionResult ChampionById(string x) { ChampionId ch = new ChampionId(); ch.Id = x; return View(ch); } 

if you can help me with this i will be thankful

3
  • 1
    what URL you are entering in browser? Commented Sep 9, 2016 at 21:15
  • You return a View named ChampionById, do you have a view with that name? Commented Sep 9, 2016 at 21:15
  • It need to be url: "Champion/ChampionById/{id}", Commented Sep 9, 2016 at 21:24

3 Answers 3

1

Forget routes.MapRoute. Just wire up all routes and then put the route as an attribute like this:

public class ChampionController : Controller { [Route("Champion/ChampionById/{id}")] public ActionResult ChampionById(string id) { ChampionId ch = new ChampionId(); ch.Id = id; return View(ch); } } 

Also x should be id. Then just remove routes.MapRoute. Then make sure you have a corresponding cshtml file called ChampionById.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks with this attribute , you force him to take the right route (this is tested and it worked )
1

Change your route to below to fit you ActionResult like below:

routes.MapRoute( name: "ChampionID", url: "Champion/ChampionById/{id}", defaults: new { controller = "Champion", action = "ChampionById", id = UrlParameter.Optional } ); 

Note what I have updated with 'id'

Here all requests with 'Champion/ChampionById/' pattern will be mapped to this route and any thing after 'Champion/ChampionById/' will be the 'id parameter'. Since it is marked as optional on the route this can be null too. So better check for it.

public class ChampionController : Controller { public ActionResult ChampionById(string id) { ChampionId ch = new ChampionId(); if( !string.IsNullOrEmpty(id)) { ch.Id = id; return View(ch); } //<TODO> : handle when your id parameter is null return View(ch); } 

2 Comments

thank you this is working for me i will edite the post = )
@MaroineAbdellah Glad it helped you. Please accept as answer if it helped you. Just had a look on your previous question you have not accepted an answer for any. This will make people not to answer you for future questions :) Please keep up the SO spirit and accept an answer. Thanks =)
1

edit your route.

routes.MapRoute( name: "ChampionID", url: "Champion/ChampionById/{x}", defaults: new { controller = "Champion", action = "ChampionById", x = UrlParameter.Optional } ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.