asp.net mvc 4 - MVC Routing News Pages -


i have managed mvc project present list of news items in seo friendly manner:

/news/ - present list

/news/newsitem/id/news-item-title - individual news item

what is:

news/id/news-item-title

exactly how stackoverflow presents questions.

however, cant seem head around how routing differentiate between 2 actions same controller action name (index).

any suggestions appreciated.

edit:

here's routes config:

routes.maproute(     "news",     "news/newsitem/{newsid}/{newstitle}",     new { controller = "news", action = "newsitem", newstitle = urlparameter.optional },     new { newsid = @"\d+" } );  routes.maproute(     name: "default",     url: "{controller}/{action}/{id}",     defaults: new { controller = "skiphire", action = "index", id = urlparameter.optional } ); 

edit 2:

this i've amended to:

route

routes.maproute(     "news",     "{controller}/{id}/{newstitle}",     new { action = "newsitem", newstitle = urlparameter.optional } ); 

controller

public class newscontroller : controller {      public actionresult index()      {          var q = _ctx.tblnews.orderby(x => x.newscreatedate)                   .where(x => x.websiteid == 2).tolist();          return view(q);      }       public actionresult newsitem(int newsid, string newstitle)      {           return view();      } } 

view - index (segment)

<table>     @foreach (var x in model)     {         <tr>             <td>@html.actionlink(x.newstitle, "newsitem", new { newsid = x.newsid, newstitle = x.newstitle.toseourl() })             </td>         </tr>     } </table> 

actionlink produces: news/newsitem?newsid=3&newstitle=my-news-item

i want: news/3/my-news-item

one way introduce additional route route configuration

routeconfig.cs:

            routes.maproute(             name: "news_seo_friendly",             url: "{controller}/{id}/{seo}",             defaults: new { action = "newsitem", seo = urlparameter.optional }         ); 

*note action value in route. need corresponding action method on controller. also, since route more specific goes above existing, more generic route(s)

an alt routeconfig.cs might safer:

            routes.maproute(             name: "news_seo_friendly",             url: "news/{id}/{seo}",             defaults: new { controller = "news", action = "newsitem", seo = urlparameter.optional }         ); 

newscontroller:

    public actionresult newsitem(string id)     {         return view();     } 

another way make "news" own area within project. gives opportunity isolate routes, if app larger, , flexibility controller name(s).

edited after feedback wanted draw attention fact parameter name on controller's newsitem() method should match being declared in route settings. in above scenario, url: "{controller}/{id}/{seo}"
should match parameter name in newsitem(string id)...or vice-versa.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -