locked
Web API newbie idiot questions RRS feed

  • Question

  • User-665371472 posted

    I  am trying to build a new Intranet site, hopefully free of MVC and WCF, both of which I don't like and don't want to know anything about.

    I want to start with an empty template with Web Forms/Web API checked. (So I can fall back on Web Forms here and there although I have long since abandoned the Post-back life-cycle in favor of JQuery/Ajax calling ASMX web services.)

    I have three early road blocks.

    1. Once I create a first Web Form, say Default.aspx, the Global.asax.cs Application_start is not called and therefore my WebApiConfig register method is not called. Found answer with Google, the process is already running, if I stop IIS Express and launch my app it hits the Application_Start
    2. I need the initial form to have a File Upload (specifically a KendoUpload) wired to an ApiController where I can digest an Excel file and return JSON to populate a grid. Just need to figure out how to have a controller return an unrelated type.
    3. How to add the route to a controller perhaps called "UploadController" that will accept a file upload.  If I've figured this out properly, api/upload translates to call a controller named "uploadController"??? It infers the "Controller" part of the name of the controller?

    I already have my code for working with the Excel file and creating the JSON I want to return (that used to be in code-behind or in ASMX services), I just haven't successfully wired up the web form (an HTML page would be OK if that's easier) to post back to the controller, especially since the routes are never called.

    I have found good example code about handling a file upload in a controller, just don't understand yet how to connect the dots (that example was in a console application).

    This is for conventional line-of-business development, dealing with a scheduling app, so I'm trying to learn exciting stuff to deal with the boredom of *what* I do.

    I'm using EF 6 and already have my Models, edmx file and database working and ready. (None of these are new to me.)

    Please help a veteran coder get away from ASMX and get RESTful Cool

    Bob Graham

    Tuesday, December 22, 2015 6:51 PM

Answers

  • User585649674 posted

    file uploading in web API.

    public async Task<HttpResponseMessage> PostStuff()
    {
    if (!Request.Content.IsMimeMultipartContent())
    {
    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    var root = HttpContext.Current.Server.MapPath("~/App_Data/Temp/FileUploads");
    Directory.CreateDirectory(root);
    var provider = new MultipartFormDataStreamProvider(root);
    var result = await Request.Content.ReadAsMultipartAsync(provider);
    if (result.FormData["model"] == null)
    {
    throw new HttpResponseException(HttpStatusCode.BadRequest);
    }

    var model = result.FormData["model"];
    //TODO: Do something with the json model which is currently a string



    //get the files
    foreach (var file in result.FileData)
    {
    //TODO: Do something with each uploaded file
    }

    return Request.CreateResponse(HttpStatusCode.OK, "success!");
    }

    Client side logic using angular js. You can do it through any javascript framework, the concept is to use fileapi to get the uploaded file and set content type as multipart = undefined. By using this method you can send both file and data related to file in a single request. This i have succesfully used in my project. I have tryied to upload PDF, zip files it is working superb.

    http://shazwazza.com/post/uploading-files-and-json-data-in-the-same-request-with-angular-js/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 28, 2015 8:17 AM

All replies

  • User-1716951449 posted

    just giving a tip about ur "idiot" question: web api is based on mvc. since u have no interest to pick mvc up, looks like u have to drop web api.

    Monday, December 28, 2015 3:52 AM
  • User585649674 posted

    file uploading in web API.

    public async Task<HttpResponseMessage> PostStuff()
    {
    if (!Request.Content.IsMimeMultipartContent())
    {
    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    var root = HttpContext.Current.Server.MapPath("~/App_Data/Temp/FileUploads");
    Directory.CreateDirectory(root);
    var provider = new MultipartFormDataStreamProvider(root);
    var result = await Request.Content.ReadAsMultipartAsync(provider);
    if (result.FormData["model"] == null)
    {
    throw new HttpResponseException(HttpStatusCode.BadRequest);
    }

    var model = result.FormData["model"];
    //TODO: Do something with the json model which is currently a string



    //get the files
    foreach (var file in result.FileData)
    {
    //TODO: Do something with each uploaded file
    }

    return Request.CreateResponse(HttpStatusCode.OK, "success!");
    }

    Client side logic using angular js. You can do it through any javascript framework, the concept is to use fileapi to get the uploaded file and set content type as multipart = undefined. By using this method you can send both file and data related to file in a single request. This i have succesfully used in my project. I have tryied to upload PDF, zip files it is working superb.

    http://shazwazza.com/post/uploading-files-and-json-data-in-the-same-request-with-angular-js/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 28, 2015 8:17 AM