same domain and one publish for web.api and mvc application? - asp.net-mvc

I have a web api returning json and I have Mvc website consuming that web api. I want to make api calls on the clientside using Jquery, not in the mvc controllers on the serverside. Therefore I don't want to publish them as 2 different application with 2 different domains as it will cause cross site scripting.
ex. mywebsite.com for mvc webapp
mywebsite.com/api for web api
Ok I figured out how to run them under same domain as explained nicely here
But I believe all of these solutions requires deploying the webservice and web api separatly even they run under the same domain?
I want to publish only mvc webapp and api should be published together with it. is it possible?
for example something like "nopcommerce" project is doing with Nop.Web and Nop.Admin? When publishin Nop.web also includes nop.admin and nop.admin is reacheable with the url like nopcommerce.com/admin

Related

how to authenticate from MVC controller into seperate Web Api project

We are going to build a web project which will potentially use Mobile app as well in the future. So we decide to consume all data works via Web Api. As a start, we will use seperate MVC project. In traditional way, I use Identity2 for authentatication issues and all Controllers of MVC project is secured by [Autherize] attribute. In our scenario, we want to give this job to Web Api project. MVC project will be only consumer of Web Api project. So I am wondering how I can login to Web Api project from MVC project and secure controllers via Web Api. Do you have any suggestions or sample?

Linking MVC controllers and Actions with Angular 2?

I really stucked in MVC + Angular 2 project in the first Step. The problem is, How to connect controllers and actions with Angular 2? Is there any routing needed?
I have done with all setups with Visual Studio 2015 to work with Angular 2. And it successfully works. But i really doing my projects in ASP.NET MVC 5 (Not ASP.NET Core). Here i dont know how the Controllers and Actions work with Angular. How can i route (navigate) to several controllers and actions in my project.
Since MVC consists of several controllers and actions. There will be many views for several actions and for every action there will be a GET(View) and POST(Form Submit) method also.
If anyone work with Angular 2 + ASP.NET MVC let me know how the Controllers and Actions are getting connected.
It looks like you are building your first SPA that communicates with a REST API.
For starters, you should get two things straight:
The ASP.NET MVC is your REST API. It will expose endpoints that can be requested via HTTP calls.
Your Angular 2 is the SPA (Single Page Application), which will make HTTP calls to the REST API you wrote in ASP.NET, and that API will give back responses of whatever you want. You can then use that response data to your liking in your Angular 2 frontend.
That being said, the first step you want to do is make sure you can properly access your API endpoints. Download a tool called Postman and test this with ease.
Ideally, you want to make this API a JSON API, because that will be the easiest thing to work with for your frontend. Plus, it's basically the de facto standard. But as always, use what works best for your use case.
Once your endpoints are working as intended, what you need to do is make some AJAX calls in your frontend that will hit those server endpoints. For Angular 2, you can use the Http package.
You will undoubtedly come across a CORS (Cross Origin Request Sharing) browser error when making the calls to your server. This is basically the browser refusing to send requests to your server until your server explicitly says you are allowed to make requests to it, assuming you are not on the same host and port.
The above was a really brief and limited-detail breakdown of what you need to do. For an example of putting it all together, check out my Angular 2/Golang chat room app. As you will see from this file, I expose some endpoints to allow create/read/update/delete actions for Todo's in my Go server. And the Angular 2 code is in this directory.
One last tip: If you are serving your front end files and exposing your API endpoints all on the same ASP.NET server, as opposed to having one ASP.NET server for the REST API, and one for the serving of UI files, then two things are true:
You need to have a catchall route in the server that will serve your index.html. You can see an example of this in this file. You'll notice a long that starts with r.NoRoute...that is where I say "no route was found so far, therefore serve index.html and let the Angular 2 handle the routing from there".
You no longer need to worry about CORS errors
Hope that helps!
Not actually, you will create and Expose APIs (WEB API) in your MVC project and your Angular 2 will consume those APIs.

How to integrate WebAPI to an MVC application

I am developing an MVC5 application and use Entity Framewerok 6 code first on this. Now we we will also develop an android application that will interact with the MVC application (CRUD operations) by using the web services. At this stage I want to be clarified about the issues below:
1) I think WebAPI is better option for us as we use the services on android apps. What do you suggest?
2) In order to integrate WebAPI to an MVC project, which changes should be made? On the other hand, can we use the same controller and data layer methods (i.e. SaveChanges, etc.) by making some modifications i.e. inheritance? Or do we have to create a seperate methods for web services? Could you give an example by code?
3) Does integrating WebAPI to the MVC project affect the MVC project's abilities or methods? I mean that is there any disadvantage integrating WebAPI to an MVC project?
Any help would be appreciated.
1) That's a good idea. Web API is easy to implement and consume
2) You don't need to make changes to intergate Web API in your application: just start using it. As you want to expose CRUD operations from EF a good idea would be to implement ODATA services. Or use something like Breeze (depending on how you want to consume the services). See "MVC and Web API" bwelow
3) Web API doesn't affect at all the MVC part, unless you make a mistake setting the routes. Although they run in the same host, they work completely independent of each other.
MVC and Web API
Unless you need to do something special, like exposing Web API in a different URL or "domain name", MVC and Web API are implemented in the same web application project. To start using Web API in your MVC project simply add a new controller. Perhaps you'll have to include also the WEB API route configuration, and some other Web API configuration.
If you want to expose the EF model throug Web API you simply have to follow the instructions in the link to create an ODATA controller, which will expose the EF model as a RESTful service, allowing you to execute the CRUD operations to the EF model through URLs.
NOTE: What you want to do is a very frequesnt pattern in MVC applications: MVC is used for generating the views, and Web API fos exposing functionalities that can be easily consumed from the views usin Javascript + AJAX. Don't be afraid to use it. You'll find no problems at all

Reference web api from a different project

I am developing a MVC app. I want to develop some web api's so i can use them from other projects.
Can this be done as currently when i do create a web api project it creates all the controllers and views etc.
Also would this be the place where you setup the types e.g. contact type which contains all the objects for a contact e.g. name, address etc.
Just need to get it right before i get my head into it.
Thanks
You could perfectly fine define your Web API controllers (deriving from ApiController) inside separate projects (class libraries). The Web API is normally RESTFul and Web API controllers don't return views. They return models which are normally XML or JSON serialized. You could even host the Web API outside of an ASP.NET application in its own host which could be a Windows service or Console application for example. You don't even need IIS. The following article described a self hosting scenario.
As far as the consuming client side is concerned, that could be absolutely any kind of application ranging from desktop application to another ASP.NET site. The client application could use the native HttpClient class to consume the WEB API or any other HTTP capable client such as WebClient or even HttpWebRequest. The client could even be written on some other non .NET platform. All that is required is the capacity to send HTTP requests.

Hosting WCF Services in ASP.NET MVC Web Application

I have an ASP.NET MVC 1.0 webapp, which serves as a front-end site for our external API. More specifically, it has a Control Panel for our API clients, documentation, etc.
Now I want to move our actual API (which is a set of WCF services) inside this project, so that, for example, http://api.example.com/controlpanel/dashboard would be served by ASP.NET MVC runtime, wherease http://api.example.com/services/1.0/users.svc would be served by an appropriate WCF service.
Granted, this can be done by adding a services/1.0 virtual folder in IIS, but I really want these two parts to be inside one project.
Is this doable at all? If yes, how do I integrate these two beasts?
Turns out MVC/WCF isn't the issue. Services hosted within the MVC app are activated just fine (I guess IIS bypasses the MVC runtime for .svc requests).
The issue was more to do with services in Areas, and requests for .svc files not going through the route table.
I've asked a more specific question addressing the actual problem here.
Expose WCF services that belong to an Area in MVC app at a routed path

Resources