locked
regarding web api and mvc routing RRS feed

  • Question

  • User264732274 posted

    web api routing code

    routes.MapHttpRoute(
        name: "ActionApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    mvc routing code

    routes.MapRoute(
                "Default",                                              // Route name 
                "{controller}/{action}/{id}",                           // URL with parameters 
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

    this part new { controller = "Home", action = "Index", id = "" } is missing in web api routing........why?

    how web api understand which action need to trigger when user issue a url. please explain in details.

    also tell me what other differences are there for mvc and web api routing.

    thanks

    Thursday, March 3, 2016 6:45 PM

Answers

All replies

  • User-782957977 posted

    By default Web Api identifies action method based on Http verbs in the request. If you want to add more than one action method corresponding to a Http verb, you need to modify default routing to include action name

    config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );

    http://www.codeproject.com/Articles/624180/Routing-Basics-in-ASP-NET-Web-API

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

    By default MVC route has action parameter and MVC identifies route based controller name, action name and id parameter in the url.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 4, 2016 1:27 AM
  • User36583972 posted

    Hi sudip_inn,

    this part new { controller = "Home", action = "Index", id = "" } is missing in web api routing........why?

    Because the API is inherited ApiController. If you want it to function in the API, you need to define a controller, which is inherited Controller. You can create a default API program, It will be created by default.

    namespace Appplication_API2
    {
        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
            }
        }
    }
    
     public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Title = "Home Page";
    
                return View();
            }
        }

    The following information for your reference.

    Difference Between MVC and Web API :

    http://www.c-sharpcorner.com/UploadFile/2b481f/difference-between-mvc-and-web-api/

    Attribute Routing in MVC and Web API - exploring the goodness :
    http://www.itorian.com/2014/03/attribute-routing-in-mvc-and-web-api.html

    Best Regards,

    Yohann Lu

    Friday, March 4, 2016 4:02 AM
  • User264732274 posted

    @Yohaan not very clear what you try to explain. i asked very simple question that why controller and action name not found in web api route but same found in mvc route. how web api understand which action he need to call when user request any.

    Friday, March 4, 2016 9:28 AM
  • User264732274 posted

    you said "If you want to add more than one action method corresponding to a Http verb, you need to modify default routing to include action name"

    would please come with sample code to show how to do it "include action name" ?

    looking for further discussion. thanks

    Friday, March 4, 2016 9:30 AM
  • User-782957977 posted

    As you have included action name in Web Api route, you can define a controller like this with 3 get methods. Default Web api route without action name will not allow you to access methods with action name.

    public class StudentController : ApiController
        {
            [HttpGet]
            public string GetStudentById(int id)
            {
                return "Student By Id " + id.ToString();
            }
    
            [HttpGet]
            public string GetStudentByFirstName(string firstName)
            {
                return "Student By First name - " + firstName;
            }
    
            [HttpGet]
            public string GetStudentByLastName(string lastName)
            {
                return "Student By Last name - " + lastName;
            }
    
        }

    You can include action method in Web Api url like this and call corresponding action methods

    http://localhost:60233/Api/Student/GetStudentById/1
    http://localhost:60233/Api/Student/GetStudentByLastName?lastName=Sam
    http://localhost:60233/Api/Student/GetStudentByFirstName?firstName=Tom

    Monday, March 7, 2016 3:02 AM
  • User264732274 posted

    your post or answer is not as per the question.

    Monday, March 7, 2016 10:25 AM