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.
Related
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 can I achieve a binding redirect in asp.net vnext? Before I would have configured the bindingRedirect using xml in the web.config file, but I can find how to to this in the Project.json
https://github.com/aspnet/Home/wiki/Project.json-file
Binding redirects are not needed in ASP.NET 5. The loader in ASP.NET 5 will automatically load the correct DLL based on project references and the available NuGet and project references.
Or, if you have a case that doesn't work, please provide more details.
According to David Fowler from the ASPNET team, binding redirects are gone and not coming back. I created a GitHub issue and he thinks this needs to be done at the Roslyn level (basically, don't output this warning anymore).
https://github.com/aspnet/Home/issues/407
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.
I am trying to add some custom functionality to ElmahR dashboard page and having some issues. I have started a new project, added ElmahR dashboard NuGet package. Configured all source applications and it is all working fine as expected.
Now I want to add some custom functionality to the dashboard page. As part of this I needed to add some UI to the dashboard page, a new Hub, JavaScript etc. The custom functionality is not related Error Logging. It is completely different. It is something we would like it to add to the dashboard page. I have added new Hub, JavaScript, HTML markups but it doesn't work as expected.
I ran into the following issues:
When i try to access /signalr/hubs, I get 404 Not found error. I searched online for this error and it usually happens if you haven't configured the Hub route. I tried to add RouteTable.Routes.MapHubs() in global.asax file. However it says that A route named 'signalr.hubs' is already in the route collection. Makes sense because I am using ElmahR and because it relies on SingalR, it would have already added it to the route collection. So what is the path to /singalr/hubs in ElmahR application? When using signalR in your standard asp.net application, the usual path to hubs is /signalr/hubs but what path is it when using ElmahR?
I am getting JavaScript error "Uncaught TypeError: Cannot read property 'client' of undefined" at the line where I am trying to access hub.client. I guess it is failing because it is not able to find the new hub that i added.
Everything works fine if I copy the hub, associated JavaScript and HTML to a different ASP.NET MVC project. For some reasons it does not work with the Project that uses ElmahR. I know there is nothing wrong with the code in Hub and JavaScript as it works fine with a standard ASP.NET MVC project. Basically all I want to do is to add a singalR hub to a project that uses ElmahR. Also I can't seem to figure out the URL path to Hubs in an application that uses ElmahR. Usually the path is /signalr/hubs
UPDATE:
After further investigation, I figured the path to hubs in ElmahR dashboard application. It is /signalr101/hubs. However when requesting this, it errors: "Uncaught Error: SignalR: JavaScript Hub Proxy generation has been disabled". It appears that ElmahR would have disabled JavaScript proxies, probably by setting DisableJavaScriptProxies property of the application's IConfigurationManager to true (https://github.com/SignalR/SignalR/pull/1222). I think because of that I am getting "Uncaught TypeError: Cannot read property 'client' of undefined in my JavaScript at the line where it is trying to access hub.client. If the JavaScript proxy is disabled, then is there any extra work I have to do in my JavaScript and Hub so that I don't get this JavaScript error.
Im writing an install and update-server for an intranet-application and I want the user to be able to install the app directly from a url such as: http://domain.local/myapp/install.exe where this request is handled by my service.
I also would like to be able to serve some basic HTML on for instance http://domain.local/myapp/status.html which is generated on the fly by my WCF-service.
Is this possible? Im using a self-hosted WS-binding WCF-service with .NET4/VS2010 on WS2008R2.
Ive found the solution! WebGet in System.ServiceModel.Web. I had to change the project target from .NET 4 Client to .NET 4 for the System.ServiceModel.Web.dll to turn up in "add reference"