Disable Razors default .cshtml handler in a ASP.NET Web Application - c#

Does anyone know how to disable the .cshtml extension completely from an ASP.NET Web Application?
In essence I want to hijack the .cshtml extension and provide my own implementation based on a RazorEngine host, although when I try to access the page.cshtml directly it appears to be running under an existing WebPages razor host that I'm trying to disable.
Note: it looks like its executing
.cshtml pages under the
System.Web.WebPages.Razor context
as the Microsoft.Data
Database is initialized. I don't even
have any Mvc or WebPages dlls
referenced, just System.Web.dll and a
local copy of System.Web.Razor with
RazorEngine.dll
I've created a new ASP.NET Web .NET 4.0 Application and have tried to clear all buildProviders and handlers as seen below:
<system.web>
<httpModules>
<clear/>
</httpModules>
<compilation debug="true" targetFramework="4.0">
<buildProviders>
<clear/>
</buildProviders>
</compilation>
<httpHandlers>
<clear/>
<add path="*" type="MyHandler" verb="*"/>
</httpHandlers>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<clear/>
</modules>
<handlers>
<clear/>
<add path="*" name="MyHandler" type="MyHandler" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
Although even with this, when I visit any page.cshtml page it still bypasses My wildcard handler and tries to execute the page itself.
Basically I want to remove all traces of .cshtml handlers/buildProviders/preprocessing so I can serve the .cshtml pages myself, anyone know how I can do this?

If you're trying to turn off ASP.NET webpages, you can set this flag in app settings:
<add key="webpages:Enabled" value="false" />

You should be able to register your own custom ViewEngine in the Application_Start method. Scott Hanselman blogged a sample that uses a custom ViewEngine for mobile devices, but the ideas should be the same for what you're trying to do.
Edit (again): David Fowler suggests:
<add key="webpages:Enabled" value="false" />
I always wondered what that setting was for, but never got around to investigating! :-)

Related

AreaRegistration.RegisterAllAreas() never finishes when using Application Insights

I created a new ASP.net MVC application using Visual Studio 2013 Update 4 and checked the box to use Application Insights. When I try and run the application (with or without debugging) the site never loads. When I debug it I noticed that it is getting stuck in Global.asax.cs on the line:
AreaRegistration.RegisterAllAreas();
I have taken a look at the answers on a few other questions including this one:
AreaRegistration.RegisterAllAreas() is not Registering Rules For Area
This did not solve my issue. I have deleted all of the content in the folders in this answer and restarted visual studio, restarted my PC and no matter what I do this method just hangs forever. It doesn't appear to just be slow, because I have waited for over 5 minutes and it still hasn't finished. Has anyone else run into this scenario and how can I fix it other than removing this call?
It appears if I comment out the Http Module registration for Application Insights then this method finishes right away, but as soon as I add them back the method hangs again. There appears to be some problem with the AreaRegistration.RegisterAllAreas() call and Application Insights.
<httpModules>
<!-- removing this makes everything work -->
<!-- <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Extensibility.Web.RequestTracking.WebRequestTrackingModule, Microsoft.ApplicationInsights.Extensibility.Web" /> -->
</httpModules>
<modules>
<remove name="FormsAuthentication" />
<!-- removing these makes things work -->
<!--
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Extensibility.Web.RequestTracking.WebRequestTrackingModule, Microsoft.ApplicationInsights.Extensibility.Web" preCondition="managedHandler" />
-->
</modules>
Yes i have the same situation and what i have done is remove appinsight element and apply xml transformation between debug and release web.config file. This helps me when me want to debug and include appinsight when release.
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Extensibility.Web.RequestTracking.WebRequestTrackingModule, Microsoft.ApplicationInsights.Extensibility.Web"
xdt:Transform="Insert"/>
</httpModules>
</system.web>
<system.webServer>
<modules>
<remove name="ApplicationInsightsWebTracking" xdt:Transform="Insert"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Extensibility.Web.RequestTracking.WebRequestTrackingModule, Microsoft.ApplicationInsights.Extensibility.Web" preCondition="managedHandler" xdt:Transform="Insert" />
</modules>
</system.webServer>
Could you provide more details, so we could reproduce this issue?
What operating system do you use (Windows Version)?
Are you running created web application on IIS Express or regular IIS?
If this is regular IIS, what application pool mode do you use (integrated/classic)?
What .NET Framework version you choose to create an application?
What MVC version your application is based on?
Did you enable WebAPI or WebForms for your web app project?

ASP.NET Ajax and "could not load type"

I have an ASP.NET web page with an Ajax file upload control. When I try to run my page, I'm told "Could not load type 'AjaxControlToolkit.AjaxFileUploadHandler' from assembly 'AjaxControlToolkit'."
Here's my control:
<ajaxtoolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" />
Here's the relevant stuff in my web.config:
<system.web>
<httpHandlers>
<add verb="*" path="AjaxFileUploadHandler.axd"
type="AjaxControlToolkit.AjaxFileUploadHandler,
AjaxControlToolkit"/>
</httpHandlers>
<pages>
<controls>
<add tagPrefix="ajaxtoolkit" namespace="AjaxControlToolkit" assembly="AjaxControlToolKit"/>
</controls>
</pages>
</system.web>
Here's my error message:
I'm running this app on Win Server 2k3 and IIS 6 and ASP.NET 4. It appears that my toolkit is the correct version.
I feel like it's probably a simple mistake, but I can't seem to find it. Let me know if more info is helpful. Thanks!
Jeremy
This is a common problem. Do a search for deleting ASP temporary files. If that does not solved it, sometimes a reinstall of Ajax using package manager sometimes resolved it.
Even I had this error
Well, I solved by removing AjaxFileUploadHandler fro Web.Config file:
<configuration>
<system.webServer>
<handlers>
<!--add name="AjaxFileUploadHandler" verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler, AjaxControlToolkit"/-->
</handlers>
</system.webServer>
<system.web>
<httpHandlers>
<!--add verb="*" path="AjaxFileUploadHandler.axd" type="AjaxControlToolkit.AjaxFileUploadHandler, AjaxControlToolkit"/-->
</httpHandlers>
</system.web>
</configuration>
This solved my error above.

Why is MapPageRoute / RedirectToRoute working on dev machine but not on production server?

I have a regular webforms project (non-MVC) and I successfully implemented a MapPageRoute and RedirectToRoute scenario. The page routing is done by database lookups because I am translating query strings (e.g. ?filter=112 to /my-friendly-url). It works nicely on my development machine running VWD 2010 Express and the included ASP.NET Development Server. The ASP.NET version is 4.0.30319.272. However, when I run the project on the production (iis7) box, I get page not found errors on the mapped pages. The rest of the site/pages run fine. My gut feeling is that the page handling is the root of the issue, but I can't figure what it is or why. It may have to do with how iis7 handles a directory vs. a filename and the interference that may arise from my program routing physical .aspx to directory-style names. My iis7 & web.config knowledge is a bit weak in this area.
Here an excerpt of my global.asax:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.Clear();
routes.RouteExistingFiles = false;
routes.Ignore("{resource}.axd/{*pathInfo}"); //ignore axd files, prevents javascript/routing collisions
routes.MapPageRoute("Education", "{category}/{section}", "~/product/education/default.aspx");
routes.MapPageRoute("EducationLanding", "{category}", "~/product/education/default.aspx");
}
Web.config: Handlers section of
<handlers>
<remove name="StaticFile" />
<remove name="PageHandlerFactory-Integrated-4.0" />
<add verb="*" path="ajax/*.ashx" name="Ajax" type="Ajax.PageHandlerFactory, Ajax" />
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="PageHandlerFactory-Integrated-4.0" path="*.aspx" verb="GET,HEAD,POST,DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
</handlers>
Thanks in advance,
John
Looking into page handling led me to the modules section of the web.config and I found my answer here on SO (IIS 7 ignores MapPageRoute without file extentions). Why I didn't find this in all my previous searches I shall not know. Behold:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
Thank you #Christoph
All this is good but do not forget the server needs to be restarted to trigger the Application_Start of your global.asax

Installed glimpse attempting to access glimpse.axd and receive 404 error?

It is a simple as that I installed glimpse following this page. :
http://getglimpse.com/About/QuickStart
I then attempt to navigate to http://myApp/glimpse.axd and receive 404 error not found.
As you can see in the Quickstart there is this statement. :
If you get a "Page not found" when browsing to "/glimpse.axd" check the troubleshooting section in the FAQ.
There is nothing in the FAQ regarding this. I have skimmed this website and getGlimpse.com attempting numerous other configurations and nothing is working. Any one else run into this issue and fix it?
Tried this also. :
Glimpse for MVC3 module not found after NuGet install of Glimpse.MVC3
I encountered the same problem and in my case the solution was to add the following code to Application_Start() in the MvcApplication class:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Make sure you have the Glimpse module and handler registered in your web.config based on the web server you are using.
If you are using a site on IIS6, in IIS7.x classic pipeline mode or Visual Studio Development Server
<system.web>
<httpModules>
<add
name="Glimpse"
type="Glimpse.Core.Module, Glimpse.Core"
/>
</httpModules>
<httpHandlers>
<add
path="glimpse.axd"
verb="GET,POST"
type="Glimpse.Core.Handler, Glimpse.Core"
/>
</httpHandlers>
...
And if you are using IIS 7.x in integrated pipeline mode or IIS Express:
<system.webServer>
<modules>
<add
name="Glimpse"
type="Glimpse.Core.Module, Glimpse.Core"
preCondition="integratedMode"
/>
</modules>
<handlers>
<add
name="Glimpse"
path="glimpse.axd"
verb="GET,POST"
type="Glimpse.Core.Handler, Glimpse.Core"
preCondition="integratedMode"
/>
</handlers>
...
</system.webServer>
I had a multi-project solution and was installing it from the Package Manager Console. I found that installing it using the following command worked:
PM> Install-Package -ProjectName <MyProject> Glimpse.MVC4
Of course you need to replace <MyProject> with your own project name.
I have had a very similar problem, and none of these options helped me, but I did get it working. This is what I had to do:
I am using MVC 5, so make sure you have read the latest config for glimpse for the version you are using. I should have been using Glimpse.AspNet and not Glimpse.Core
My web config looks like this:
<handlers>
....
<remove name="Glimpse" />
<add name="Glimpse" path="glimpse.axd" verb="GET"
type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet"
preCondition="integratedMode" />
</handlers>
<modules>
....
<remove name="Glimpse" />
<add name="Glimpse"
type="Glimpse.AspNet.HttpModule, Glimpse.AspNet"
preCondition="integratedMode"/>
</modules>
I am using IIS Express, Vs2015 and for some reason my C:\Users\me\Documents\IISExpress\config\applicationhost.config got messed up, and had a special entry for Glimpse.
So I found and removed any entries with Glimpse in them (carefully, you might want to comment them out instead)
<application path="/Glimpse.axd" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="\path\to\extra\website" />
</application>
I think this may have happened from a really early version of glimpse been installed, and also something to do with the upgrade to MVC5, but not 100% sure why...
Hope this helps someone else.
In my case, the web app is not deployed in root, so the url is:
http://localhost:54026/MyApp/glimpse.axd
Very obvious, but I'll leave this answer as a reminder.
Install Glimpse.AspNet and it will fix the issue for you and also modify the web.config by adding the handler and module.

HttpHandler Redirect

I want to write an HttpHandler to redirect traffic to various webpages on the server.
The user will type in http://www.thisissupposedtoberedirected.com/site12 and should be redirected to the appropiate site, in this example site version 1.2
I know how to program in ASP.NET and C# but I don't seem to grab the finer detail about website management.
How can I manage to get this done? What should I do in the web.config? I've read this msdn page but it isn't much help.
HttpHandlers are actually fairly simple components.
First, you need to create a class that inherits either IHttpHandler or IHttpAsyncHandler (for your use, I'd suggest IHttpHandler since there's really no heavy lifting being done).
You then compile the DLL and drop it in the bin folder of your web application.
Now the tricky part. Deploying HttpHandlers in the web.config file is tricky since it's different between IIS6, IIS7 Integrated Mode, and IIS7 Classic Mode. The best place to look is this MSDN page:
How to: Register HTTP Handlers
IIS6
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly" />
</httpHandlers>
<system.web>
</configuration>
IIS7 Classic Mode
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly" />
</httpHandlers>
<system.web>
<system.webServer>
<add name=SampleHandler" verb="*" path="SampleHandler.new"
Modules="IsapiModule"
scriptProcessor="FrameworkPath\aspnet_isapi.dll"
resourceType="File" />
</system.webServer>
</configuration>
IIS7 Integrated Mode
<configuration>
<system.webServer>
<handlers>
<add name="SampleHandler" verb="*"
path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly"
resourceType="Unspecified" />
</handlers>
<system.webServer>
</configuration>
As you can see, each IIS configuration requires entries in slightly different sections of the web.config file. My suggestion would be to add entries in each location so that IIS configuration changes don't break your HttpHandler.
1) You need to create a new class that implements IHttpHandler or IHttpAsyncHandler (the latter being when you are very comfortable with managing your own threads). Create your logic there.
2) Then, modify the web.config so that you register your handler:
<system.web>
<httpHandlers>
<add verb="*" path="*.htm" type="System.Web.StaticFileHandler"/>
<add verb="*" path="*.html" type="System.Web.StaticFileHandler"/>
<add verb="*" path="*.ico" type="System.Web.StaticFileHandler"/>
</httpHandlers>
</system.web>
This is a sample setup in my web.config - yours may vary slightly.
Now, your HttpHandler should be registered and based on the details provided in your registration in the web.config, when you request certain URLs, you will be mapped to the Handler you created instead of the normal ASP.NET Page Handler.
Additionally, for your specific issue, I wouldn't recommend writing a HttpHandler - I would just do a DNS redirect, or do some lookup in your OnInit code to check the Host Url, and if it is one specified in your DB, you do the redirect yourself based on the configuration data.

Categories

Resources