Send data from web API controller into separate asp.net mvc website - c#

I have the following situation:
There are two different projects in our solution:
asp.net mvc website
asp.net web api (service for mobile apps).
Users should have an ability to upload profile pictures from their mobile apps using mobile web api project. But the problem is that images are physically stored on different server at asp.net mvc website.
So here is the flow:
User uploads picture and sends it to mobileapidomain/api/user/uploadpicture. (base64)
Now I have to send this image to asp.net mvc site and store it on asp.net mvc server.
What is the best way to send current base64 string with image from web api controller into asp.net mvc project?

In such situation I think the best option would be to install within your mvc web application project packages for WebApi and make this project web application (normal usage) and web service at the same time. Then create new ApiController like this:
[HttpPost]
public IHttpActionResult UploadPicture(string base64)
{
// Do something with image
return Ok();
}
If you do not wish to install WebApi libraries there then I would suggest adding new regular Controller for uploading pictures and adding there similar action:
[HttpPost]
public void UploadPicture(string base64)
{
// Do something with image
}
If something goes wrong within the acton throw exception to inform peer that something wrong is going on. If the file is too long to send in one post I would suggest splitting it and concatenating within mvc application as it receives new portions of it.

Related

Asp.net Web Api Configuration

I am new to asp.net mvc world. So I have a question
I have already developed a web app using Asp.net MVC (also deployed on production).
Currently I am working on mobile apps. For this I need web services (restful).
For Restful web services do I have to make a new project (within existing solution) or can I incorporate Restful webservices into my existing (Asp.net MVC) project ? (I prefer 2nd option if possible)
If I have to make new project for Web Api, then how will I deploy both projects on production knowing that Web Api project is dependent on Asp.net-MVC project ?
One thing you need to understand first is whether it's a web service,wcf service or a Web API the only thing you need is to get a json/xml output which you can use in your mobile app.
Let say you have and asp.net mvc application which has some action methods, but you might be returning a View or PartialView which is not you want for a mobile app to parse. So you need to create an action method which returns JsonResult.
If you want to use all the RESTFul verbs like POST,PUT,GET,DELETE you can add another controller which inherits from APIController and write methods there, but either ways output is same.
So it's up to you what to do and how to proceed, only thing is with an APIController you will have some more verbs and code ahve some special returns like "Ok" e.t.c

Structure of asp.net with web api and mvc controller

I have a question about the structure of asp.net website, especially back-end stuff
Currently, I have sql database, web api controller and mvc controller.
I have been using web api for getting or posting data to database, and using mvc controller for directing webpage.
What I want to do is as followings
It is basically registration process
call web api method to post user email and password from mvc controller
web api controller processes the user email and password, using stored procedures and functions
if the user email already exists in the database, web api returns some custom status code? to let the mvc controller know that.
Here is problem, since the web api will be used by other devices as well so I can't directly do some business logic for dealing with the duplication of email. So I just want to let somehow the MVC controller know the error and do some business logic.
How can I do that?, Is it fine to return some status code like 101, 201 to MVC controller? then How can I do that??
Thanks guys
Use standard HttpResponse with a code from public enum HttpStatusCode
Under the link you'll find the list of all codes and some examples how to use and consume them;
for example, in your API controller may return:
return new HttpResponseMessage(HttpStatusCode.NotAcceptable);
and then on the consumer side you'll have to handle that error.

ASP.Net MVC Website & Web API As Single Implementation

I have a client dashboard application, written in ASP.Net MVC 4 (not bound to this version, happy to upgrade). The website is hosted in Windows Azure.
Our clients would like to programmatically access this dashboard, via an API.
Affectively they would like to be able to perform all of the same functions which they normally carry out on the dashboard website, programmatically from code using an HTTP Restful Service.
....My instant reaction was to simply build an ASP.net Web API project, and separate out the shared services/libraries/components from the existing MVC project so that both the API and the MVC website can call the same code base.
Questions
Is it possible to simply create Web API controllers within my existing MVC website project, and expose them over HTTP?
If it is not possible to do "1.", will Azure play nicely if I have an MVC solution, a separate Web API solution, and a shared library project of common services and models? How will I ensure that Azure copies the shared library components into the cloud when I deploy the MVC solution and the Web API solution separately?
Update
Based on the comments, the following implementation of two separate controllers (the original MVC controller within the MVC project, and an additional Web API controller within the same project), does in fact work. Please note that based on the following LINKED ARTICLE, the below implementation would be a "pre-MVC 6" implementation. MVC 6 itself provides the ability to implement both API calls and normally MVC View calls, into a single controller (as opposed to separate controllers that inherit from different base classes).
public class ProductsController : Controller
{
//Products/Index
public ActionResult Index()
{
return View();
}
}
public class ProductsAPIController : ApiController
{
// GET api/productsapi/getall
public IEnumerable<string> GetAll()
{
return new string[] { "value1", "value2" };
}
}
To be fair - MVC or WebAPI, as far as your consuming clients are concerned, it shouldn't matter.
Both MVC and WebAPI can create JSON or XML outputs - it's just one makes it a bit easier (WebAPI serializes the relevant response based on the client).
But you can quite easily mix the two together, as others have said, it's as simple as Add New Item
The newest version of MVC (6) has now integrated both technologies into one solution. http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6
This would be ideally what you are looking for if you can start a new project.
Question 1: Yes it is, just take a look at the article above.
Question 2:
Azure will be fine if you have a MVC solution and also a WebApi solution, but I think you will need to run them under different websites (I'm not 100% on this).
Yes it will work fine. The only potential gotcha is you have to clearly separate your WebAPI routes from your MVC routes (like, for instance having all WebAPI routes under a /api/... prefix, this is the default).
If you do decide to do two separate projects for whatever reason and want to have a shared library, the best solution is to keep that library in a NuGet package on a private feed. TeamCity supports this out of the box, along with other nice features (continuous integration/deployment).

Using MVC5 and Web API 2 in the same project

I'm about to start a new project in ASP.NET MVC5, which has a bit of Web API too. I'll also need a Windows Forms client which will call the API. This client has a file system watcher that detects when a file has been changed, and will post the contents to the API.
When the API receives the data, it does some calculations, and ideally will send the results through SignalR to the browser and update the display.
I'm getting rather stuck trying to work out the authentication. I want to use Individual User Accounts, so the user can log in with the Windows Forms client (and get a token) and in the browser to view the data.
I've got as far as File -> New -> Project, and tried an MVC project with the Web API box checked, and a Web API with the MVC box checked. Looking at the two AccountController classes that these generate, they seem quite different.
I guess the options are
Try to get these two controllers working together
Call the MVC controller from the Windows Forms client
Have two projects in the solution and try to work out how to use SignalR to talk between them.
A better way?
I suspect the last one. I've not used Web API before, so I could be doing this all wrong. What approach should I take?
I would say, create 2 different projects, 1 for MVC 1 for API.
Use 1 BLL which is referenced in both of them and carries the logic for both of them and will not be dealing with separate controllers.
Of course if you need other layers like DataAccess or Repository, you have to create them once and they will be referenced in the BLL which is later referenced in both MVC and API interfaces.

How to upload text to an ASP.NET MVC 2 Action from a Windows Application

I'm new to asp.net mvc and web development in general and would like to know the best way to pass text from a windows application written in c# to a asp.net mvc website so it can be stored in a database. Is it possible to call a controller action (via a url similar to REST) from the Windows application or do I need to create a web service? If someone can point me to a verbose example that would be great.
Thanks
You can use the WebClient class. Look here for a simple sample.
Hope this helps.
Why do you want to pass it to your MVC site to store in the database? How are you interacting with your database in the MVC site?
If you have two different applications that need to access the data you should put it in a separate layer and may be create a WCF service or a Web Service.

Categories