2

I have web api project and I have *.html page and I need run it after run my project but I don't know how do it. In MVC I use MapRoute like this:

  routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

But I don't know how do it in web api. I have next routes config:

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

and how to add other config for run my html page? Thanks.

2 Answers 2

2

This code how you add new routing:

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

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

Now just write route to your static file

Sign up to request clarification or add additional context in comments.

3 Comments

Thnaks, but how to render html page. Somethink like this return View("Index.html"); ? In web api
Oh, ....It is bad idea. API build for return data. If you try return view - probably you have bad architecture. You can try some tricks in google, but again - it is bad idea.
Not problem. Good luck!!
1

I use Like this code into the WebApi project:

public class DefaultController : ApiController
    {
        /// <summary>
        /// Default controller for render swagger UI 
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("")]
        public RedirectResult Index()
        {
            var requestUri = Request.RequestUri;
            return Redirect(requestUri.AbsoluteUri + "swagger/ui/index");
        }
    }

Comments

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.