Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net WebPages website - How to add WebApi controller

I am building my site using Asp.Net Webpages 2 and Razor (C#).

I tried to insert WebApi controller and all I get is 404. Controller is a default one.

I even tried adding

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

and I still get 404.

What else is there to do ?

like image 220
exe.bat Avatar asked May 12 '26 03:05

exe.bat


1 Answers

I did it. In Global.asax you need to add

<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.Web.Http" %>

And in Application_Start

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

Now it is working like it should. It did not work before because I tried the same code in Global.asax.cs.

like image 127
exe.bat Avatar answered May 13 '26 19:05

exe.bat