Asynchronous web application with ASP.NET MVC 4 and Angularjs - c#

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.

Related

How to create Razor pages web app + web service

I'm mostly back end developer and want to start small side project.
I want to do razor pages web app and as a part of it I would like to have web api that can be consumed outside of the app and if I want, use it to build other front end with it.
What would be good project structure? I don't want to have it deployed as 2 apps. One for razor, second for web API. I would like to have it as a one deployed app.
Is this good approach?
Thanks
I tried separate razor pages app and separate web api. Deployed as two pages.
Since you're building a Web API project anyway, you should consider making a frontend application with a framework like Vue that interacts with the API, instead of a Razor project. Having both a Razor project and a Web API project would be redundant as both of them would have to incorporate most of the same backend code.
It better separates the role of the frontend and backend applications, and Microsoft's documentation suggests using this project structure too.
You'd have to deploy two separate apps, but this would be a more suitable approach.
If you still want to deploy one application only, you could just incorporate Web API services in your Razor app, but this really isn't recommended.

Angular UI for a WebAPI in .NET Core while still the primary purpose is a WebAPI, not a frontend App

Any feedback much appreciated.
I need to create a Web API (ASP.NET Core Web API), which does some basic CRUD operations. Thats no issue at all, and I need to create a simple Angular UI for the Web API as well. Thats also no issue. But is there a way to combine both into a single application ? WebAPI mainly and an optional UI part in Angular than having a second angular solution ? I know there is a visual studio project template where you create a .NET Core Angular Application, if I do that, how would I expose the API at the sametime to other clients as API, while still managing the Angular UI ?
Put in another way, my .NET Core application should be primarily a WebAPI which should work independently and act like a WebAPI, while an optional part of it should have an Angular frontend connecting to the WebAPI.
You can view this as a matter of security for your WebAPI and how you expose connections to it. You can serve your API endpoints under one route, i.e. /api and you Angular application at you site route or host it elsewhere.
If you follow the same practices for exposing the WebAPI cross origin, then your Angular application can consume the API in exactly the same way as any other client would.
Followed #johnny's comment and it works !
All you have to do is compile your angular project and put it under wwwroot. Then you can navigate to your index.html page, set your webapi startup to serve static files and your done

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.

ASP.NET MVC combined with Web API or..... Nancyfx?

I've never used traditional ASP.NET MVC and started with Nancyfx. I'm considering making the switch to ASP.NET MVC + Web API but have some general questions.
I think I may have been spoiled with Nancyfx but I have some real problems and concerns with it which is why I'm not sure on it for my rewrite. The main problem is the load times. I understand it compiles the Razor views upon first load which extends the load times but there are bugs that cause it to not work (known issues). I've implemented a work-around but I've read that ASP.NET MVC can pre-compile to avoid this situation (Nancyfx cannot).
Doing some research with ASP.NET MVC + Web API it seems the Web API is for the restful endpoints while the ASP.NET MVC is for serving the pages. As you know, Nancyfx combines these two technologies into one which is what is really nice. Am I correct about this or is there a way around to make ASP.NET MVC serve the Razor pages plus serve the JSON/XML requests depending on the request type?
I do not really care to use ASP.NET core to be honest because my application integrates heavily into Active Directory, Microsoft Exchange and other Windows application that are not on other platforms. Someone wanting to deploy it on a Linux server is kind of pointless IMO for what I'm doing.
To put it simply, ASP.NET MVC offers you two controller base classes: Controller and ApiController. You inherit from the Controller class if you want to create view-based controller actions and you inherit from ApiController if you want to create API actions. On the other hand, in ASP.NET Core, you can use the same controller infrastructure to deliver views as well as REST APIs. You can always combine .NET Framework and ASP.NET Core. However, .NET + ASP.NET Core solution works only on Windows about which you apparently have no problem.

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