How to migrate existing Asp.net application to WebApi - c#

Hello Stackers,
I have an Asp.net Mvc and i wanna migrate the index page from it to an Web Api
i wanna know if there is any pattern or approach to follow for making it more easy.
i google it but i didn't found a useful answer.
Any answer will be appreciate it.
Thanks

I am not sure if there is a pattern, but next some steps that could help.
Group all your services that returns, saves, delete or do some work in the backend. On your backend you could create a new layer (New project or new namespace) to call the existing ones and export those services as Web API endpoints.
On your frontend check how are you going to consume your Web API services. I mean if you are going to call it directly with javascript or maybe using some library or framework.
Those are going to be the first steps.

Related

Webservice call MVC 6 with razor vs WebAPI

This seems to be a newer topic or i haven't understood it. I have build a website using core 2.2, mvc and razor. I have my CRUD model functions in my controller class.
Now I want to call these functions from my website(which works fine) and from my xamarin app.
Having read lots of tutorial, the way to go is to put a webapi in between. But lots of sites indicate that mvc and webapi have merged in .net core.
What is the best practice for my xamarin app to communicate with my website?
Although you don't need a web API but you cannot call your ActionResult from xamarin app. if your controllers' methods return an object instead of a view then MVC will take care of serializing it to JSON for you so this way you can call it from your app. for more information check this article.
I am currently working with same methodology, I have new website and new mobile app which is built in Xamarin.
I have created a separate project in WebApis. Now I can use same Apis in both projects. So no need to write new code for separate projects. Just write simple json based Apis and use in both projects.

c# web api 2 - any way to wrap all routes for "site is down"?

The high level problem I'm trying to solve:
Given our design of
- c# web api to modify entities and retrieve view models
- react SPA frontend
I want to be able to conditionally turn the c# web api "off" and tell the SPA the "site is down for maintenance". The purpose for this is to prevent odd errors in the client in the case someone is using the site during the transition.
The initial idea I had to solve this was to wrap each call in a web.config check to see if the site is down, and if so, return a hard-coded error response which the frontend knows what to do with. However the cleanest approach I can think of is to inject a service into each web api controller and invoke it at the beginning of each route. I was instead hoping for a way to avoid this boilerplate.
In node I would decorate all the routes in a wrapper function that does this. C# is a little more foreign to me.
If you host your WebAPI as its own ASP.NET site then you can place an app_offline.htm file in the IIS root, and it will automatically go into maintenance mode.
ASP.NET will immediately server 503's and the page contents for all new requests, while allowing existing requests to complete.

Is there such a thing as an entirely empty website project?

I'm looking at working my long-standing API to run on IIS rather than in a desktop app as it is now. Everything on the API is working so I'd rather not change too much if I don't have to. I know about the new Web API template in ASP.NET MVC 4 and I've worked with it, but I found that it didn't give me the control over everything that this particular project needs.
So my question is, is there any way to build an application for IIS that has something like an entry-point where I can just get a web request then use entirely my own code from there? Or do I have to build something that uses the Web API?
Yes, you'll want an ASP.NET handler.
How To Create an ASP.NET HTTP Handler by Using Visual C# .NET
http://support.microsoft.com/kb/308001
You'll need to handle parsing the request and serializing the result yourself. It's probably much better to create a web-api facade in front of your services than trying to do it manually.

Asynchronous web application with ASP.NET MVC 4 and Angularjs

We are planning an update for a web application implemented using ASP.NET Web Form. We'd like to inctroduce MVC pattern, so we are basically oriented to MVC 4.
We are also evaluating AngularJs, that seems a great MVC framework for web development.
I've read a bit about using AngularJs + Web Api, but I have no experience about Single Page Applications or asynchronous applications. For example, how they implement authentication?
I'd like to know if there's a well known architecture for asynchronous application developing, and how can I implement this with MVC 4 + AngularJs.
Any suggestion?
instead of using WebAPI, take a closer look at www.servicestack.net
AngularJS with servicestack backend -> works like a charm! i'll never switch back to webapi!
AngularJS is a great MVW (Model-View-Whatever) framework and already provides a lot of "architecture" for developing a web app. Therefore, I am not sure why you would want to use MVC4 + AngularJS. AngularJS has asynchronous built in - $http and $resource can be used to make asnync calls.
They also recently added animation support making web animations super easy to implement. So there is a lot that AngularJS has already built in and one just needs to become familiar with it. I suggest you look at some sample applications such as this one.
There are plenty of guides around for doing so. Since WebApi is a RESTful service you can use angularjs $http or/and $resource for consuming it.
Additionally there are libraries out there for consuming RESTful services which work as a middle man for frameworks like Angular and MVC. Check out This Visual Studio template.

Can an asp.net mvc application also have a web service?

I have an asp.net mvc application and now I need to add a web service to go along with it. What is the best solution for this? Just create a new web service project and add it to my solution then deploy it to the same web server using a different url? I would like it to be a part of the mvc application only because I have all my database code in there.
Any suggestions would be appreciated. Thanks.
There's no reason not to add a web service project.
You state that all your database code is in your MVC project. I strongly recommend you remove it from there into a separate class library project. This third project would be used both by the web service and by the MVC application.
I also strongly recommend that you not use ASMX web services for any new development.
Use WCF only, unless you have no choice at all. There's a misconception that WCF services don't do SOAP - they do, and WCF has replaced ASMX.
Web service could mean a soap based web service or a RESTful web service. I can't think of any reason why you would not be able to simply add an asmx file to your project and be in business. That is the soap based route. If you want to be really cool though you can simple return xml from a controller action and implement a RESTful solution right over the MVC framework.
If you want to use a regular ASP.NET asmx web service, it's certainly possible. Here's an example from Scott Hanselman that does just what you are asking about and it throws in some other ASP.NET technologies for good measure.
All you have to do is File -> New Item -> Web Service and it should work like a regular ASP.NET application in your Mvc project.
i think there's a couple of things here.
you can indeed add a web service to an MVC application. you may even consider identifying the web service(s) as a script service to make REST like operations easier to perform via javascript. this may not be necessary due to your circumstances.
i think there is a stronger question as to the underlying architecture. If you are placing the web service withing your mvc application, because, your database code is already there...should it be? it might be a good time to abstract your data layer out a little. However, if you're dealing with a relatively small project and don't need the flexibilty, then certainly, add a web service right in. i guess what it really boils down to is addressing the true needs of your application.
MVC is built on the asp.net framework. You should be able to include a web service within the same project. I haven't done it but I know that you can combine asp.net forms applications with MVC applications in the same project. The same should go for web services.
Unless your application is very small I would recommend you create different projects for each logical part of the application. A good staring point is having a project for each of these:
Domain objects
Data access
Web Services
UI (your ASP.NET MVC app)
This provides a clean separation corresponding to your architecture and supports reuse.

Categories

Resources