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.
Related
I have an ASP.NET Core Web API application that is separate from my ASP.NET Core MVC application (UI). I didn't want to combine them for several reasons since in the future many different clients might need to access the API.
The MVC application makes calls to the API two different ways:
1. Using System.Net.HttpClient in the MVC controllers to populate the View Models.
2. When making jQuery AJAX calls I figured it would be best to directly call the API. (Note: I enabled CORS to allow calls from the MVC application). This would be the same thing as an Angular client making calls directly to the API.
My questions:
Is what I am doing pretty common making calls to the API directly from the client with jQuery and additionally System.Net.HttpClient in the Controllers when not using AJAX and just loading the view data. Should the AJAX calls go to the MVC controller first then let them MVC controller make a DELETE request using System.Net.HttpClient so I am never directly calling the API from the client?
What would the security concerns be? I am a bit nervous calling the API directly from jQuery. I feel like anyone can pretent they are localhost:6000 (my MVC port) and make a DELETE request to localhost:6001/api/users/1 (my API port).
You absolutely correct to be concerned about this. You should be securing your application from this making sure any requests to access these API methods are authorized.
I recommend reading 10 Points to Secure Your ASP.NET MVC Applications. for an overview on how to protect your application from this kind of attack and several other common security vulnerabilities.
I am new to ASP.NET MVC and Web API and trying to get the basics. AFAIK, we have project templates in VS 2013, named as MVC, Web API and Both of them together.
I have gone through the tutorials and learned that we can make an API by using MVC alone as well as with Web API Template.
So, What are the differences between these, based on Architecture and Usage?
Basically, a Web API controller is an MVC controller, which uses HttpMessageResponse as the base type of its response, instead of ActionResponse. They are the same in most other respects. The main difference between the project types is that the MVC Application project type adds web specific things like default CSS, JavaScript files and other resources needed for a web site, which are not needed for an API.
MVC is used for creating web sites. In this case Controllers usually return a View (i.e. HTML response) to browser requests. Web APIs on the other hand are usually made to be consumed by other applications. If you want to allow other applications to access your data / functionality, you can create a Web API to facilitate this access. For example, Facebook has an API in order to allow App developers to access information about users using the App. Web APIs don't have to be for public consumption. You can also create an API to support your own applications. For example, we created a Web API to support the AJAX functionality of our MVC web site.
Microsoft changed the way they present the different templates. Now instead of using different templates for different project types, they encourage developers to mix ASP.NET technologies inside the same project as needed. Microsoft calls this vNext.
UPDATE: For ASP.NET Core, Web API has been integrated into the MVC 6 project type and the ApiController class is consolidated into the Controller class. Further details at: https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6
My two cents...
In ASP.Net MVC – the MVC’s Controller decides what should be the View - i.e., the controller decides what the user should “see” (based on the current scenario or context), when they make a request.
In ASP.Net Web Forms, the ASPX pages decides what the user should “see” when they make a request.
But in Web API, there is no control/power to any of the Web API’s features to decide what the user should “see” when they make a request.
Web API is NOT a technology tied up with websites only. It can be used for multiple purposes – not only websites. So it doesn't know the meaning of rendering
Further Reading
Planning Web Solutions Today: Web Forms, ASP.NET MVC, Web API, and OWIN.
WCF or ASP.NET Web APIs? My two cents on the subject
The Next Generation of .NET – ASP.NET vNext
Getting Started with ASP.NET MVC 6
MVC controller derived from controller class. In Mvc you can returns views. Mvc achitecture uses to create an application. However Web apis are used to provide data to various application.
Web Api drives from Api controller and it doesn't return view.
Note: You can also create Web Api from MVC controller but you need to return result as JsonResult or other web api supported return types.
In addition to answers already provided here, its worth noting any controller which inherits from ApiController and having an action with Http verb POST can only have one [FromBody] input parameter. If using a MVC controller (deriving from 'Controller') you can have many post input parameters.
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
I am new to ASP.NET MVC and Web API and trying to get the basics. AFAIK, we have project templates in VS 2013, named as MVC, Web API and Both of them together.
I have gone through the tutorials and learned that we can make an API by using MVC alone as well as with Web API Template.
So, What are the differences between these, based on Architecture and Usage?
Basically, a Web API controller is an MVC controller, which uses HttpMessageResponse as the base type of its response, instead of ActionResponse. They are the same in most other respects. The main difference between the project types is that the MVC Application project type adds web specific things like default CSS, JavaScript files and other resources needed for a web site, which are not needed for an API.
MVC is used for creating web sites. In this case Controllers usually return a View (i.e. HTML response) to browser requests. Web APIs on the other hand are usually made to be consumed by other applications. If you want to allow other applications to access your data / functionality, you can create a Web API to facilitate this access. For example, Facebook has an API in order to allow App developers to access information about users using the App. Web APIs don't have to be for public consumption. You can also create an API to support your own applications. For example, we created a Web API to support the AJAX functionality of our MVC web site.
Microsoft changed the way they present the different templates. Now instead of using different templates for different project types, they encourage developers to mix ASP.NET technologies inside the same project as needed. Microsoft calls this vNext.
UPDATE: For ASP.NET Core, Web API has been integrated into the MVC 6 project type and the ApiController class is consolidated into the Controller class. Further details at: https://wildermuth.com/2016/05/10/Writing-API-Controllers-in-ASP-NET-MVC-6
My two cents...
In ASP.Net MVC – the MVC’s Controller decides what should be the View - i.e., the controller decides what the user should “see” (based on the current scenario or context), when they make a request.
In ASP.Net Web Forms, the ASPX pages decides what the user should “see” when they make a request.
But in Web API, there is no control/power to any of the Web API’s features to decide what the user should “see” when they make a request.
Web API is NOT a technology tied up with websites only. It can be used for multiple purposes – not only websites. So it doesn't know the meaning of rendering
Further Reading
Planning Web Solutions Today: Web Forms, ASP.NET MVC, Web API, and OWIN.
WCF or ASP.NET Web APIs? My two cents on the subject
The Next Generation of .NET – ASP.NET vNext
Getting Started with ASP.NET MVC 6
MVC controller derived from controller class. In Mvc you can returns views. Mvc achitecture uses to create an application. However Web apis are used to provide data to various application.
Web Api drives from Api controller and it doesn't return view.
Note: You can also create Web Api from MVC controller but you need to return result as JsonResult or other web api supported return types.
In addition to answers already provided here, its worth noting any controller which inherits from ApiController and having an action with Http verb POST can only have one [FromBody] input parameter. If using a MVC controller (deriving from 'Controller') you can have many post input parameters.
i need to write website, that must have private part in single page application and public part that must be in classic style(to the best indexing by searching engines) with Ajax support without usage CORS or JSONP. All logic must be in service (RESTful) style, for future mobile apps support. So i decide to use ASP.NET MVC and ASP.NET Web API in one project(to prevent CORS or JSONP), i planning to use asp.net mvc controllers to return template html for private part and whole html for public part. I don't want to put any logic to asp.net mvc controllers except get some data (dictionary collections) for public part views. So is see two possible solution for that.
Create ApiController and try to get data from it method
Don't like that, because it destroy RESTful style and SoC (it's need to pass IUnityOfWofk implementation to asp.net mvc controller constructor under DI, to redirect it to constructor of ApiController)
Like that because it don't need to spent time to http request same host (localhost).
Get data under http request to ApiController in MVC controller.
Like that beacuse RESTful and SoC is OK.
Don't like that becuase it spent time to http request to same host(localhost).
My questions are:
What else possible solutions can be to solve my task (in .Net platform) ?
How http request to same host (localhost) in asp.net mvc controller may influence the delay in time for the client browser (public part), it is criticaly for response time ?
What is the best solution 1 or 2 ?
Another option is to factor out your logic from the Web API controller to a separate class model and use it both in the MVC and the Web API controllers, e.g. by injecting the instances of the relevant logic classes into the controllers. You'd also be able to share the Unit of Work.
Of course, this adds another layer of complexity, but at the same time leads to a better Separation of Concerns. After all, MVC and Web API are only a way to present your logic to specific clients. In case of Web API, the main responsibility of the Web API layer is to create a RESTful interface to your logic; handling the logic itself is not a task of the Web API layer if you are very strict.
I'd favor this approach over the others as these approaches have some downsides (numbers as in your question):
Create ApiController: you need to mimic the Web API environment very well. Though this is possible, it also means that changes on the Web API side of things will have an effect on the implementation of your logic.
Make a HTTP request to the Web API:
Though a request to the same server in the MVC controllers might be relatively quick and therefore not critical, it will take some time that is not necessary if you can just have a call to some shared classes.
Also, the HTTP request adds a point where your system might break.
In addition, you wouldn't be able to share a Unit of Work with the Web API.