web services - Am I reinventing the wheel with this ASP.NET Web API based webserver? -


in standalone (selfhosted) application, have httpserver on single base adress can either serve simple web pages (without serverside dynamics/scripting, returns content request files) or serve restful webservices:

  • when http://localhost:8070/{filepath} requested, should return content of file (html, javascript, css, images), normal simple webserver
  • everything behind http://localhost:8070/api/ should act normal rrestful web api

my current approach uses asp.net web api server both html pages , rest apis:

 var config = new httpselfhostconfiguration("http://localhost:8070/");  config.formatters.add(new webformatter());  config.routes.maphttproute(    name: "default web",    routetemplate: "{filename}",    defaults: new { controller = "web", filename = routeparameter.optional });  config.routes.maphttproute(    name: "default api",    routetemplate: "api/{controller}/{id}",    defaults: new { id = routeparameter.optional }); 

the webcontroller controller serves web pages naive code:

public class webcontroller : apicontroller {     public httpresponsemessage get(string filename = null)     {         /// ...         var filepath = path.combine(wwwroot, filename);         if (file.exists(filepath))         {             if (hascssextension(filepath))             {                 return this.request.createresponse(                    httpstatuscode.ok,                     getfilecontent(filepath),                     "text/css");             }              if (hasjavascriptextension(filepath))             {                 return this.request.createresponse(                    httpstatuscode.ok,                    getfilecontent(filepath),                    "application/javascript");             }              return this.request.createresponse(               httpstatuscode.ok,                getfilecontent(filepath),                "text/html");         }          return this.request.createresponse(             httpstatuscode.notfound,             this.getfilecontnet(path.combine(wwwroot, "404.html")),             "text/html");     } } 

and again, behind /api, controllers normal rest apis used.

my question is: on right track? kind of feel rebuilding webserver here, reinventing wheel. , guess there lot of http request web browsers make not handle correctly here.

but other option have if want self host , @ same time server rest web apis , web pages on same base address?

looks trying recreate asp.net filehandler self host. there better solution though. using katana(an owin host) hosting layer web api. owin supports hosting multiple owin frameworks in same app. in case, can host both web api , file handler in same owin app.

filip has blog post on started here. can use configuration code this,

public void configuration(iappbuilder appbuilder) {     // configure web api.     var config = new httpconfiguration();     config.routes.maphttproute("default-api", "api/{controller}");     appbuilder.usewebapi(config);      // configure static file handler.     appbuilder.usestaticfiles(); } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -