Search code examples
asp.net-mvcurlroutesrazorengine

Tow way for call a action in asp.net mvc with route attribute - Routing In asp.net mvc


All i want 2 way for call a action . for example

http://localhost:16800/Content1/1/text

and

 http://localhost:16800/Content1/1

and my routeconfig is default routeing. i use from route attribute .

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

and my controller used route attribute so :

namespace WebApplication2.Controllers

{

 [RoutePrefix("Content1")]
[Route("{action=Index}")]
public class Content1Controller : Controller
{

    [Route("{id}/{text}")]
    public ActionResult Index(int id, string text)
    {
        return View();
    }
}

} Now , this way working for me . http://localhost:16800/Content1/1/text

and this way not working for me. http://localhost:16800/Content1/1 i just to use both way for call my action. Remind that i use [Route("{action=Index}")] on the my controller i don,t need define Action name in my url.


Solution

  • Try this, It will work(tested).
    
    
    namespace WebApplicationExamples.Controllers
        {
            [RoutePrefix("Content1")]
            [Route("{action=Index}")]
            public class Content1Controller : Controller
            {
    
                // GET: Content1
                [Route("{id}")]
                [Route("{id}/{text}")]
                public ActionResult Index()
                {
                    return View();
                }
            }
        }