0

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.

1 Answer 1

1
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();
            }
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

i test your code in a test project . and this work fine. but it,s not work my main project. :(
i get this error A public action method '1' was not found on controller
oh. i use HttpGet and this work fine.[Route("{id}")] [Route("{id}/{title}")] [HttpGet]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.