Webservice call MVC 6 with razor vs WebAPI - c#

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.

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

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.

Connect to an MVC application from a winforms application

I have written a C# MVC4 internet application and have a question in relation to calling some of the ActionResult methods.
How can I call any of the ActionResult methods from a different application other than the MVC application?
What I am wanting to do is create a Winforms application, connect to the MVC application and then call some of the ActionResult methods.
Is this possible? How should I do this? What resources should I research into?
Thanks in advance
It's not ideal to use MVC 4 as a restful host because it is designed to be rendered to HTML.
You will instead want to use Web API. It's designed to be consumed by clients.
You can abstract the logic from the MVC project to a shared project and re-use the functions for the Web API.
Here is a great article about writing a client to interact with Web API: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

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.

Categories

Resources