locked
Routing question - DefaultApi vs Default RRS feed

  • Question

  • User1266884534 posted

    My understanding is that the route for a webapi project is determined by the code in webApiconfig.cs file. I deleted the home controller from  my webapi project.

    I wish to point the default route to HelpApi instead of the home controller. So I did two things.

    1) Deleted the routing code in RegisterRoutes method of the routeconfig.cs

    2) Modified the code in Register method of the webAPiconfig.cs

    This is how the two files look in my project.

    webApiConfig.cs

            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
    
                // Web API routes
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "HelpApi",
                    routeTemplate: "api/{controller}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }
    

    RouteConfig.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace SampleWebApi
    {
        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            }
        }
    }
    

    When I run my project, the project starts with a 403 instead of routing the browser to the HelpApi pages. Here is the error page that is being displayed in the browser.

    HTTP Error 403.14 - Forbidden

    The Web server is configured to not list the contents of this directory.

    The services underneath are working and accessible from POSTMAN. I just want the user to see https://localhost:44315/Help instead of the 404 or 403 forbidden.

    How can I write the code to do this?

    Friday, August 2, 2019 7:28 PM

All replies

  • User1120430333 posted

    404.14 means that IIS Express is not configured for directory browsing.

    https://stackoverflow.com/questions/8543761/how-to-enable-directory-browsing-by-default-on-iis-express/12637943

    You could push the WebAPI project out to local IIS and test it that way too.

    https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2017

    But on the other hand, you could have left the WebAPI as is and added the new controller to the project an routed normally.

    You should be able to set the WebAPI project as the startup project in VS and start the project up in debug mode and do URL stuff at the browser, like accessing the ValuesController 

    Sunday, August 4, 2019 10:03 AM
  • User1266884534 posted

    Hello,

    I want to the default url to launch the Help page instead of the home controller. I am aware of enabling directory browsing. I don't wish to enable directory browsing.

    Thank You!

    Tuesday, August 6, 2019 4:08 PM
  • User-474980206 posted

    then you need to map a route to this page, or set your start url.

    you routing requires

       <hostname>/api/<controllername>

    if you want

      <hostname>/

    to go some where you need to define a route for this.

    Tuesday, August 6, 2019 4:37 PM
  • User1266884534 posted

    Hi Bruce,

    Which class should I make this change in?

    Thank You!

    Tuesday, August 6, 2019 4:55 PM
  • User-474980206 posted

    you register the new route in either webApiConfig.cs or RouteConfig.cs depending what type of controller hosts the help page.

    note: I'd use swagger rather than a help page myself. then you just make the swagger page the default route.

    Tuesday, August 6, 2019 8:18 PM
  • User1266884534 posted

    Bruce,
    This is an internal application for the company I work for. I don't believe swagger is free. I will have to get a buy-in from the higher ups.

    The trouble with the VS 2019 templates is that it doesn't let me host just a webapi project. It has added the MVC flavor and that is causing some grief.
    I am not even sure which controller should I change. Both webapiconfig.cs and routeconfig.cs exist in my project structure.

    I wish someone on the ASP .NEt team would look at this question.

    Thank You!

    Wednesday, August 7, 2019 3:00 PM
  • User475983607 posted

    I don't believe swagger is free

    Swagger is free.

    The trouble with the VS 2019 templates is that it doesn't let me host just a webapi project. It has added the MVC flavor and that is causing some grief.
    I am not even sure which controller should I change. Both webapiconfig.cs and routeconfig.cs exist in my project structure.

    I wish someone on the ASP .NEt team would look at this question.

    A Web API template exists.  Create a new project but select the Empty project option.  From there check the Web API option and create.  The project will contain configuration only.  Add a controller and set the defaults as you wish.

            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
    
                // Web API routes
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "{controller}/{id}",
                    defaults: new { controller= "Default", id = RouteParameter.Optional }
                );
            }
        public class DefaultController : ApiController
        {
            // GET: api/Default
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            // GET: api/Default/5
            public string Get(int id)
            {
                return "value";
            }
    
            // POST: api/Default
            public void Post([FromBody]string value)
            {
            }
    
            // PUT: api/Default/5
            public void Put(int id, [FromBody]string value)
            {
            }
    
            // DELETE: api/Default/5
            public void Delete(int id)
            {
            }
        }

    Wednesday, August 7, 2019 6:07 PM
  • User-474980206 posted
    We can not tell how to route to your help page, if we don’t know what the help page is. Is it a mvc view, razor page, static html page, link to another site?
    Thursday, August 8, 2019 2:23 PM