简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM