How to debug MVC web app with rest api controller backend? [duplicate] - asp.net-mvc

In VS 2012, I am attempting to create an MVC 4 web application with jQuery calls to a Web API project. (Other devs will be consuming the API with our current, native app, and probably adding to the API in the future.) So I have one project that is the Web API, and another project that is the MVC 4 website. I can only set one of them to run, and they use localhost:xxxxx.
How do I debug changes to both? For example, let's say I add a new API path /api/customer/get and then a new jQuery ajax call to that path and do something with the resulting JSON. I've changed code in both projects and want to follow it end-to-end; how do I launch both? How do I debug both?
Just to be clear, the MVC app isn't making server-side calls to the API, I'm using MVC mostly to be able to easily use bundling, minification, and (hopefully) pre-compiled Handlebars templates in .NET; the API calls are coming from jQuery. As I am still relatively new to these technologies, alternate suggestions are welcome.
Thank you in advance.

I had the same problem and have found a solution from here:
forums.asp.net
The fix is to do the following:
In your solution file, click properties go to the Startup project node (if it is not already selected)
Next select Multiple startup projects. Select your website and your webservice and in the Action column make sure both of them have "Start" selected.
Now when you debug your website and put a break point in your webservice, it should hit the break point.

Coming late to the party but in case anyone else is looking for a solution, this is what was best for me: Set the Api project up to be the starting project (I needed to limit to one startup so that I could flip between browsers more easily). After firing up the service project, right click on the web/ui project and select debug, start new instance. You'll have both running and you'll seamlessly step from web to api.

I had a similar problem with my web api project. My solution consisted of an angular front end with 2 web api projects on the backend. One web api project handled "authorization" and the other handled "resources". I used the following tutorial by Taiseer Joudeh as a starting point:
http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api/
Breakpoints worked on the "authorization server"... but not on the "resource server". I compared the packages from the two projects to see what was different. Once I added "Microsoft.AspNet.WebApi.Cors" to the "resource server" project, the breakpoints starting working.

Related

Can IT Hit WebDAV Lib be used with ASP.NET Web API 2 in the same Project?

I´m currently evaluating Web Dav Product for .NET and I´m really impressed. I only noticed that when I´m integrating the sample code (Asp.NetFileSystem) into my existing ASP.NET Web API 2 Project, it doesn´t work with Office Word directly as a save target.
We have Web API 2 with Windows Authentication and CORS Headers on.
Windows Explorer can open and Navigate the WebDAV Folder and I can create new files there. But when I´m saving from word directly, I get errors in Word that the network path does not work / exist. I tried to debug it but it happens
I separated the WebDAV part in it´s own project for test purposes and it works without the problems.
Am I using it wrong in the context of Web API 2, or is this an unsupported usecase?
I´ll try to get fiddler logs to.
Thanks a lot!
Thanks to the IT Hit WebDav we were able to trace the problem down. To work correctly, OPTION requests have to be processed by the WebDAVHandler. We have a custom module which intercepts OPTION requests to handle CORS. Because of our Module, the WebDAVHandler was not able to add the necessary headers, and Office opened the files readonly.

Browser Links - Nonstop Signalr Calls in MVC 5 application

I've created a new MVC 5 Project in VS2013 and when I create the project I gave the authentication mode to none because I planned to use signalr in my project later.
I didn't install any signalr related files instead of the two signalr js files (in my scripts folder). And there are no any startup class or OWIN.
But when I run my application there are signalr calls going and going using longpooling. These things can be viewed in the firebug console.
Previously I've used signalr with many projects using VS 2012 MVC4 this is the first time I'm using MVC5. And this slows down my application. How this could be happened.
Here is the POST message...
Please help..
This is a dev tool only, it's not actually part of your application. I suggest you read this intro which also tells you how to disable the feature.

Is it possible to remove all .Net MVC components in a .Net Web Api application so that I can use AngularJS as the front end?

I'm creating an application with a .Net Web Api project wanting to use pure AngularJS as the client side. Since Web Api is built on top of MVC, it creates MVC specific and default items that I feel is not needed. These items include the HomeController, _ViewStart.cshtml, _layout.cshtml, etc. I tried removing them but it comes up with errors. Has anyone tried to remove the MVC stuff out of the web api project and used separate client side front-end? Is it even possible to remove the MVC items without errors?
Remove RouteConfig.cs from App_Start, remove the Views directory and all sub-directories including the Views internal web.config file. Comment out or delete all the lines in the Global.asax.cs Application_Start method except GlobalConfiguration.Configure(WebApiConfig.Register). Remove the HomeController, add an index.html and any needed Angular scripts and go at it. I also added solution folders to organize my views as reusing the existing Views folders did not work. I'm using VS 2015 but is should work for 2013 also. PWE
Web API is not built on top of MVC.
The default templates bring in MVC for the sake of supporting a help page, but you don't need to use it.
You can start with an empty web project and just check Web API.
The routing piece is server routing and it's part of what maps the URL to Controllers+Actions, it has nothing to do with Angular routing.
As Mike Cheel alluded to, there are no dependencies between MVC and Web API. However, if you use the built-in templates, it's easy to get the impression that the 2 are linked. They include a lot of stuff in these templates because they can't anticipate where you want to go with your project... so they try to cover all the bases.
For your purposes, you would probably be better off to start with an empty project and add only the components that you actually need. For this approach, some of the best tutorials and starter projects are from Taiseer Joudeh's "Bit of Technology" blog. His tutorials helped me to build an "MVC Free" web application from scratch that uses JSON Web Tokens and AngularJS Interceptors for security and Web API 2 and Entity Framework to serve up the data.
He has many tutorials on his website... but you might want to start with "AngularJS Token Authentication using ASP.NET Web API 2, Owin, and Identity". What what.. you didn't ask about security? Well... security is an issue that you will need to confront at some point anyway... and Taiseer presents a nice solution for securing an Angular/Web API application.

Calling Web API from MVC Application when both in same Solution

So I am totally new to the Web API and I thought to myself I would start a test project to learn it and AngularJS and how they can work together nicely ... etc.
I have created a multi-tier architecture within my solution, and inside my TPlan.API project, I have the following route configured in the Global.asax
GlobalConfiguration.Configuration.Routes.Add("default",
new HttpRoute("api/{controller}"));
TPlan.WEB is my MVC application, and it is set up as "Start Up Project". When I run it, I want to be able to go to:
mysite:port/api/test
And get the value from the API from my test controller in there.
However it is not working for me, I get the following:
No HTTP resource was found that matches the request URI 'mysite:port/api/test'.
Er, in visual studio, right click on the solution and go to properties.
Go to startup and select the "multiple projects" option and tick the website and the webservice to both start up.
That will start both of them. But as has been pointed out, they're running on separate ports, they're completely separate applications...
I don't think you can register a route that belongs outside of the website.
We work like this
View => POST / GET => MVC Controller => HTTP Request => API Controller
So our mvc views post or get to our mvc controllers, and then we fire off a separate http request to the web api. this is a server to server call, its like calling any external web service. we wait for the response from the api and then return whatever is required to the view...
What you are attempting isn't logically possible without installing your WebAPI project into IIS ahead of time, which I'm sure is not what you want. Both projects cannot be run at the same time, as only one project will be able to launch a debug session of IIS Express. Even if both projects were able to run at the same time, they would be on different logical ports, and routing from one project would have to be manually sent to the listening port of the other instance. Basically, your Global.asax on your API project will never run, as that project will be built as a class library.
Make your TPlan.API project a simple Assembly, reference it from TPlan.Web and pass the GlobalConfiguration.Configuration property over to a Register method that is in your API assembly. When the MVC project starts, both the MVC routes and the Web Api routes will be active on the same web host.
Here is an example of an API that has both a console host and a web host in the same solution.
Please check the following site. I believe the issue lies in the configuration of the route
http://www.asp.net/web-api/overview/extensibility/configuring-aspnet-web-api

Using the razor engine in SharePoint 2013 Visual Web Part

I'm trying to make use of the razor engine in a SharePoint 2013 Web Part. This, I read, shouldn't be too difficult since SharePoint 2013 Visual Web Parts can be built using .NET Framework 4.0
I'm simply not sure of where to begin to do this. I've seen an article on uploading the .cshtml file onto a document library and referencing the document library in the Web Part, but it is a bit unclear.
Does anyone have an idea of how I can start? Or point me to a step-by-step guide to achieve what I am after.
What my end goal is: Take a piece of an MVC application, customize it and make it a Web Part. I need to be able to modify or access SharePoint content on that site which is why I cannot use an App Part.
Okay.
First install the Office Development Templates from
http://www.microsoft.com/visualstudio/eng/office-dev-tools-for-visual-studio
This will give you the following template selection on reopening Studio
Then navigate to wherever EwsManagedApi32.msi is and enter the following command in an elevated prompt -
EwsManagedApi32.msi addlocal="ExchangeWebServicesApi_Feature,ExchangeWebServicesApi_Gac"
Now your templates are usable and the one you want is "VS2012 Web Part".
Now, by default this will add an ASP.NET project, which is not what you want, so manually delete the ASP.NET project, add an MVC one, highlight the SharePoint project in Solution Explorer, hit F4, and select the MVC project in the "Web Project" dropdown at the bottom of the properties.
And there you go. One controller per web part, sorted.
I am sharing link SharePoint MVC webpart that uses Knock out framework using MVC for SharePoint. It has step by step instructions which might help you.
Hope this will be a good starting point
http://www.codeproject.com/Tips/739445/SharePoint-Razor-View-WebPart

Resources