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

Amin Saadati

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.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to generate URL for the action with attribute routing in Asp.Net MVC

ASP.NET MVC Attribute routing

Mvc's ASP.NET Routing integration: Controller/Action-to-Route-Resolution

ASP.NET MVC Routing - Url.Action sending query string not route values

ASP.NET MVC 5 Attribute Routing: Url.Action returns null

Asp.Net MVC5 - Html.Action - Attribute Routing - Weird Behaviour causing Exceptions

ASP.net MVC routing

Parse to object with a route like ASP.NET MVC routing

ASP.NET Core 3 MVC endpoint routing and localization by route

ASP.NET Core 3.1 Routing - API and MVC Route confusion

Asp.net Mvc Routing. "*" meaning in route

How routing works with default controller and action in asp.net mvc

Asp.Net MVC 5 custom action routing under an area

Hide routing values coming from action in ASP.NET MVC?

ASP.Net MVC @Url.Action() routing to the wrong controller

Asp.net mvc routing without a controller or action name

Asp.net core MVC make action optional in routing

Asp Net Mvc Routing without action on multiple Controllers

Controller level parameter with ASP.NET MVC attribute routing

Default attribute routing not worrking in ASP.NET MVC

ASP.NET MVC Web API Attribute Routing With overloads

ASP.NET MVC - Attribute Routing not finding area view

Asp.net MVC 5 Attribute Routing Constraint

ASP.NET MVC Attribute-Routing with Null Values

ASP.NET MVC Attribute routing parameter issue

Route Issue ASP.net MVC 4 not redirecting to Index Action

ASP.NET 5 MVC 6: Route to action by Accept header

ASP.NET MVC 5 custom route goes to wrong action

Add custom word in the URL of Route attribute ASP.NET MVC