c# - How to Use MVC Controller and WebAPI Controller in same project -
i trying use mvc controller , webapi controller in same project got 404 errors webapi . started project mvc project in vs 2015 added webapi controlle , default code giving 404 error
what possible solution . have tried solution on stackoverflow didn't worked 1 of them below link (accepted answer on there ) all asp.net web api controllers return 404
global.asax file code :
protected void application_start() { arearegistration.registerallareas(); globalconfiguration.configure(webapiconfig.register);//web api 1st filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); }
webapi.config file
public static class webapiconfig { public static void register(httpconfiguration config) { config.maphttpattributeroutes(); config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } ); } }
route config file code
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 } ); } }
webapi controller code
using system; using system.collections.generic; using system.linq; using system.net; using system.net.http; using system.web.http; using o365_apis_start_aspnet_mvc.models; using microsoft.identitymodel.clients.activedirectory; using o365_apis_start_aspnet_mvc.helpers; using system.threading.tasks; namespace o365_apis_start_aspnet_mvc.controllers { public class mailapicontroller : apicontroller { private mailoperations _mailoperations = new mailoperations(); //async task<backofficeresponse<list<country>>> // get: api/mailapi public ienumerable<string> get() { return new string[] { "value1", "value2" }; } // get: api/mailapi/5 public string get(int id) { return "value"; } // post: api/mailapi public void post([frombody]string value) { } // put: api/mailapi/5 public void put(int id, [frombody]string value) { } // delete: api/mailapi/5 public void delete(int id) { } } }
also getting nuget restore error in same solution error nuget failed restore png
you need register routing web api before registering routing mvc, app_start()
function should this:
protected void application_start() { arearegistration.registerallareas(); globalconfiguration.configure(webapiconfig.register);//web api 1st filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes);//mvc 2nd bundleconfig.registerbundles(bundletable.bundles); }
Comments
Post a Comment