asp.net mvc - Web Api Pdf download and routing -


so trying lean web api , implement in project.

there 2 things not quite sure about

  1. i trying download pdf. web api in app server , consuming in web server.

in web api have method in controller

public class filescontroller : apicontroller {             [actionname("getfile")]     public httpresponsemessage getfile(string filename, string ceqrnumber, string latestmilestone)     {         var file = getlistoffilesbyceqrandmilestone(filename, ceqrnumber, latestmilestone);         var path = file.filepath;         var extension = file.fileextention;         httpresponsemessage result = new httpresponsemessage(httpstatuscode.ok);         var stream = new filestream(path, filemode.open,fileaccess.readwrite);         result.content = new streamcontent(stream);          if (extension == ".pdf")             result.content.headers.contenttype = new mediatypeheadervalue("application/pdf");          if (extension == ".xlsx" || extension == ".xls")             result.content.headers.contenttype = new mediatypeheadervalue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");          if (extension == ".zip")             result.content.headers.contenttype = new mediatypeheadervalue("aapplication/zip");          return result;     } } 

now not sure how consume it. in main mvc app in web server. have action method

public actionresult getfile(string filename, string ceqrnum, string latestms, string token) {     string url = "https://xxx/api/files/getfile/" + filename + "/" + ceqrnum + "/" + latestms;          using (var client = new httpclient())         {             client.defaultrequestheaders.accept.clear();             client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/pdf"));              task<string> response = client.getstringasync(url);              // not sure how convert response pdf          } } 
  1. the second question have in routing: in webapiconfig.cs there default route set up

     public static void register(httpconfiguration config)  {     config.routes.maphttproute(         name: "defaultapi",         routetemplate: "api/{controller}/{action}/{id}",         defaults: new { id = routeparameter.optional }     );  } 

i noticed if have have diffrent parameters have add in config

so added

config.routes.maphttproute(             name: "getfilelist",             routetemplate: "api/{controller}/{action}/{ceqrnumber}/{latestmilestone}",             defaults: new             {                 ceqrnumber = urlparameter.optional,                 latestmilestone = urlparameter.optional,             }         );          config.routes.maphttproute(             name: "getfilefordownload",             routetemplate: "api/{controller}/{action}/{filename}/{ceqrnumber}/{latestmilestone}",             defaults: new             {                 controller = "files",                 filename = urlparameter.optional,                 ceqrnumber = urlparameter.optional,                 latestmilestone = urlparameter.optional             }         ); 

now, can have 1 default route work irrespective of parameters , action name or every different action , parameters have write new route.

i appreciate answers. trying understand concepts of web api first time using it.

thanks

a few comments -

  1. don't open file read/write, read plenty good.

  2. the second route fine don't need first one, can alternatively post following route:

api/{controller}/{filename}/{ceqrnumber}/{latestmilestone}

and remove [actionname] attribute getfiles.

even simpler can use following route api/{controller} , pass parameters query string.

so it's going this

config.routes.maphttproute(     routetemplate: "api/{controller}" ); 

your url going like:

http://xxx/api/files?filename=foo&ceqrnumber=bar,&latestmilestone 

the controller code:

public class filescontroller : apicontroller {             public httpresponsemessage getfile(string filename, string ceqrnumber, string latestmilestone)     {         var file = getlistoffilesbyceqrandmilestone(filename, ceqrnumber, latestmilestone);         var path = file.filepath;         var extension = file.fileextention;         var result = new httpresponsemessage(httpstatuscode.ok);         var stream = new filestream(path, filemode.open,fileaccess.read);         result.content = new streamcontent(stream);          // header work         return result;     } } 

for client code instead of readasstringasync use

 using (stream responsestream = await response.content.readasstreamasync())  {      // read response stream      await readresponsestreamasync(responsestream, filename);  } 

and don't forget validate response code on client

for comment on how save file stream:

 private void readresponsestreamasync(stream responsestream, string filename)  {       using (var filestream = file.create("c:\\path\\to\\" + filename))       {             responsestream.copyto(filestream);       }  } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -