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
Related
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.
I´m a newbie in ASP .NET MVC technology .
Actually my question is:
Is a PostRepository, used in ASP .NET MVC web app (like this) working as WebService? Or basicaly, when I have ActionResult method in Controller i.e. for deleting some post from db based on PostId as paramether of this method (/post/delete/5 for example) is this web service? Or it´s just a mechanism that MVC use for performing CRUD operations so it´s not a webservice?
It´s true, that ActionResults methods of controller returnig Views are not web services?
The repository pattern is helping you to keep a clear code structure. It explains you how and where to keep the database access functions.
This way you don't overcrowd your code in the controller. Plus you can recycle the code, calling the same function from two different places.
For creating rest web services you should use Web API. And the result of an web service is not an view; it is usually an object.
Returning a View means that you want to display a page and not a service.
I am building simple LOB application (online restaurant reservation application) and i want to support multiple client side application types like web site, windows desktop application, android application etc. So i think that best way to design system is to separate ASP.NET 5 WebApi which will provide interaction with database, authorization and stuff like that and than build separate solution with projects for client applications which will consume that API. One of those client applications would be Silverlight application. Problem is, how i consume my API from Silverlight application. I can't find any information to link Silverlight with ASP.NET 5. All i got isMVC6. Is it better to createASP.NET MVC6application, implement my APIs there and hostSliverlight` in that project or to separate it like i described?
Silverlight is dead, but it can easily consume REST API so why cannot it work with ASP.NET Core 1.0?
There is no need to have special documentation.
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.
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.