Get glimpse working with web api - asp.net-mvc-4

I have an MVC4 project with the template Intranet Application. Glimpse is working on the MVC4 Routes but not on web api routes (the footer vanishes).
I'm thinking of creating an action in a MVCController which takes a request as parameters to instanciate the right WebApiController and calls the right action with the parameters.
Is there a better alternative ?
I tried to use this (found here) but it doesn't work :
<inspectors>
<ignoredTypes>
<add type="Glimpse.AspNet.Inspector.RoutesInspector, Glimpse.AspNet"/>
</ignoredTypes>
</inspectors>
</glimpse>

The issue you mention is related to Glimpse making changes to the Routes configuration which broke the WebApi Help Pages, fortunately that issue has been solved in the meanwhile.
Based on your question, I assume that you want to see Glimpse diagnostics for WebApi related calls? Unfortunately Glimpse v1.8.4 does not support WebApi requests.
The good news is that support for WebApi is being added as we speak. You can check the pull request to see how it's going and when it's expected to be released.

Related

Visual Studio Code only offers Weather Forecast proxy

I am not able to run any other controller in the default React app for .NET Core 6.0. Has anyone figured this out? I believe I saw a quick message on the terminal about the weatherforecast route being in the SPA proxy.
Whenever I try to go to any other route for a controller it redirects to the fallback page. I have routing setup with default settings. Just seems an odd behavior.
It's obviously a proxy issue because if I run the controller from the IIS port 7132 instead of the 44402 (spa server), I am able to retrieve the json response. There's quite a long readme file in here I can plod through but maybe someone has a better solution.
Update: I found the solution, well there were several, from setting CORS policies on the server side, etc. The easiest way was to do it "pre-flight". I found a handy file called setupProxy.js (who knew lol), where I was able to set an array of end points to proxy. And this had the /weatherforecast endpoint already specified. So now it all works! I guess I answered my own question.
const context = [
"/weatherforecast","/account"
];

Why is app.UseMvc() added when creating a Razor Pages Project?

I'm new to ASP.NET Core and Razor and I apologize if this is insanely obvious somewhere.
After doing some research, I elected to use Razor Pages over MVC as I thought there would be more benefits and liked the idea of the code behind page models etc.. I chose ASP.NET Core Web Application -> Web Application and create a new project targeting ASP.NET Core 2.2.
The default template appears to create a "pure" RP project - eg. The Pages folder instead of MVC's Controller/Models/Views.
I'm getting really confused because there are still elements of MVC in the RP project - eg. app.useMvc() in Startup.cs, the inclusion of _ViewStart.cshtml, the use of ViewData["x"].
Realistically this is fine and one can press on but then following RP guides and supposed conventions seems to go haywire - eg. _PageStart.cshtml doesn't actually work from my testing.
Where am I going wrong - am I reading into it or caring too much? Hopefully someone can enlighten me! Thanks in advance for trying :)
razor pages, whilst they do not use controllers, are actually part of the mvc framework and require you to call app.UseMvc() in order to initilise everything it needs including routing etc.
You can also use UseMvc to do some configuration to your application:
app.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.Add("options");
});
More information on this can be found at https://learn.microsoft.com/en-us/aspnet/core/razor-pages/razor-pages-conventions?view=aspnetcore-2.2

How do I access RouteTable.Routes.MapHttpRoute?

I have a Web Forms app that I created a few months ago and I added a Web API controller. I tried to use the 'automatic' routing that I saw in a presentation recently, but all I got was a 404. Then I tried to add routing for the Web API controller in my Global.asax using MapHttpRoute, as I've seen in several tutorials. However, even after adding Imports System.Web.Http to the file, the project will not recognize RouteTable.Routes.MapHttpRoute() I have tried adding other namespaces and ensuring that I have all the necessary Nuget packages, but I still am unable to set up routing for the Web API controller. Does anyone have a recommendation of where to start?
If anyone has this same issue in C# read on.
I am also using a web forms app and set the routing through the Global.asax file. For me Intellisense was 100% correct and it would not build regardless. To get this to work I had to add the following using directives.
using System.Web.Http;
and
using System.Web.Routing;
Becareful not to use using System.Web.Http.Routing by accident. This wont work.
You should add the reference to System.Web.Http.WebHost assembly and make sure you have
using System.Web.Http;
Why ? MapHttpRoute is defined in two assemblies, in System.Web.Http:
public static System.Web.Http.Routing.IHttpRoute MapHttpRoute(
this System.Web.Http.HttpRouteCollection routes, string name, string routeTemplate)
Member of System.Web.Http.HttpRouteCollectionExtensions
and in System.Web.Http.WebHost
public static Route MapHttpRoute(
this RouteCollection routes, string name, string routeTemplate, object defaults);
First one is an Extension on HttpRouteCollection
Second one is an Extension on RouteCollection
So when you have a webforms app, your Routes are defined in a RouteCollection so you need the WebHost version.
Its because architecture allowing WebApi to be hosted also out of IIS. see Hosting WebApi
I found this thread, which indicates that IntelliSense apparently has a problem with this, but if you type something like the following, it will build and run:
RouteTable.Routes.MapHttpRoute("MyApi", "api/{controller}")
I've just created two new webforms apps (one using .NET 4, the other 4.5), did nothing more than add web api via nuget and it worked.
What version of ASP.NET is your app running in? If you're running ASP.NET WebForms 2.0/3.5 then it's not supported.
Here's a tutorial which demonstrates how to add Web API to your Web Forms app - http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-aspnet-web-forms
I have the same case, I solved by creating the api controller, when you add the
api controller VS will load all required refrences.

Web API Routing - Returning 404

I have a Web API project in MVC 4 but cannot find the URL to my web service. Rather than MVC, I am actually just using the C (controller) as I'm not returning a view and my model is located in an external project.
I'm using the default route, which is api/{controller}/{id}. The name of my controller is RESTController (I know, probably wasn't the best name) and is located at ~/Controllers/RESTController.cs
Based on naming convention, it seems that my web service should be located at localhost:port/api/REST but I'm just getting a 404 resource cannot be found error (no XML representation of the object returned or anything). Is this the expected behavior without a view?
The funny thing is that I also have a SOAP API in an external project that is actually just returning the REST API result and it works as it should. Doing unit tests on my methods passes... I just can't access it from a browser. I've tried every url imaginable.
Basically...
Would there a way to debug this? (ie. Turning on ASP.NET directory listing)
Does not having a view effect what is shown in the browser?
Shouldn't the browser return an XML file representing the object?
Am I missing something obvious?
Nevermind, I'm a moron. I found the problem... At some point I accidentally drag and dropped (drug and dropped past tense?) my Global.asax file into my Views folder... therefore my routes were not getting registered.
Working as intended now.

Wrongly redirecting to /Account/Login?ReturnUrl

I have been working on a classic .Net web forms (4.0 framework) solution for my current project. Since the project is too big to upgrade to MVC, I simply added a MVC project to the solution and created all my REST services in the MVC controllers.
It was working fine for several days and randomly I started getting redirected to Accounts.Login?{ReturnUrl}. My web forms app doesn't have an Account folder the logical conclusion was that the MVC project was interfering somehow.
After checking all the configs, IIS settings, and debugging from the Global.asax to find out that I had no clue what was redirecting; I started doing a source control compare to see what changed where.
I found the below line had been added to the appSettings tag in my web config. After removing it everything started working correctly.
<add key="webpages:Enabled" value="true" />
After a bit of searching I found this article...
what is the function of webpages:Enabled in MVC 3 web.config
...which explains a little, but not why this setting would cause the site to redirect away from my default document to a URL that doesn't exist in either project or why my MVC project is being called at all.
Can anyone point me in the right direction?