Adding HttpHandler to MVC application - asp.net-mvc

I have an MVC4 application to which I added an HttpHandler:
<system.web>
...
<httpHandlers>
<add path="Files" verb="*" type="MyHttpHandler" />
</httpHandlers>
</system.web>
I also ignored the relevant path in RegisterRoutes so that the requests to "Files" are not handled by MVC:
routes.IgnoreRoute("Files/{*pathInfo}");
The problem is that the MyHttpHandler is invoked only for requests to "Files", not for any of its children or sub-folders.
I've tried using the <location> element, but getting that to work means that you will be hard coding the application's virtual path in the "path" attribute (e.g., <location path='MyApp\Files'>).
What is the correct method to use to allow all requests for "Files" and any of its sub-folders (and sub-folders of those folder, etc) to get routed to MyHttpHandler?

Scratch that...<location> seems to work OK. Though, you need both the <web> and <webServer> entries to ensure that it works for both IIS and the Visual Studio Development Server. For example:
<location path="Files">`
<system.webServer>
<handlers>
<add name="MyHandler" path="*" verb="*" type="MyHttpHandler" />
</handlers>
</system.webServer>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="MyHttpHandler" />
</httpHandlers>
</system.web>
</location>

Related

asp.net core app. 500 - Internal server error

I uploaded my website to godaddy windows hosting with plesk.
All files are uploaded but I get an error:
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
From researching the problem online I figured the prob;em must be in my web.config file
I am showing here what I have in my web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false" allowOverride="false">
<system.webServer>
<httpErrors errorMode="Detailed"/>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\myApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
<system.web>
<trust level="Full" />
</system.web>
</location>
<system.web>
<customErrors mode="Off" />
</system.web>
</configuration>
Your host (GoDaddy) will need to setup their servers to support asp.net core
web.config in asp.net MVC core Project
I tried on myproject also, but it didn't work
So after trying everything including calling godaddy and searching all over, I found out that GoDaddy doesn't support asp.net core 2.0 applications
I hosted with 1and1 windows hosting and it runs!

Cannot run Glimpse with ASP.NET MVC 5 Application on IIS

I have an ASP.NET MVC 5 application that is running on IIS and I want to use the Glimpse Package so that I can monitor all of the SQL calls that are being made within my application.
I have installed Glimpse using the Nuget Package Manager Console by using the command:
Install-Package Glimpse.MVC5
Since my application is running on IIS, I added the following commands to my web.config file:
<configuration>
<configSections>
<section name="glimpse" type="Glimpse.Core.Configuration.Section, Glimpse.Core" />
</configSections>
</configuration>
<system.web>
<httpModules>
<add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet"/>
</httpModules>
<httpHandlers>
<add path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet"/>
</httpHandlers>
</system.web>
<system.webServer>
<modules>
<add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" preCondition="integratedMode"/>
</modules>
<handlers>
<add name="Glimpse" path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" preCondition="integratedMode" />
</handlers>
</system.webServer>
After these commands are in place and I try running my application through IIS, I get the following error:
Could not load file or assembly 'Glimpse.AspNet' or one of its dependencies. The system cannot find the file specified.
I know that Glimpse is really set up to work by building your application locally but I think you can get it to work on a remote server as well.
Any help would be greatly appreciated!
I eventually got Glimpse up and running by making sure I installed all of the necessary NuGet packages for it.

Javascript bundling in another folder

How can I have JavaScript bundling working from another folder (aside from the Script folder). If I do this:
bundles.Add(new ScriptBundle("~/bundles/search").Include("~/Views/Search/*.js"));
The browser tells me the javascript file can't be found. Is it possible to do this
or do all my sripts have to be in the Scripts folder?
Basically I want my Javascript included in my View subfolders
You need to change web.config in Views folder according this answer:
In ASP.NET MVC, how can I load script from my view folder?
Good example from Ashley Lee:
<system.webServer>
<handlers>
<add name="JavascriptViewHandler" path="*.js" verb="*"
preCondition="integratedMode" type="System.Web.StaticFileHandler" />
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*"
preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
Since you specifically want to only include javascript files, make the following change to your ~/Views/web.config file, by adding the "JavascriptViewHandler" section.
<system.webServer>
<handlers>
<add name="JavascriptViewHandler" path="*.js" verb="*"
preCondition="integratedMode" type="System.Web.StaticFileHandler" />
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*"
preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
This will preserve all of the current blocking for non-javascript files.
I don't know if i understood your question properly, but if you want to use a script file from any folder in a View or preferably in it's Layout, you can add the following tag in <head> section of you View or _Layout.cshtml:
<script src="#Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
You can mention the complete path to your script file instead of ~/Scripts/jquery-ui.min.js

asp mvc IIS7 less/sass empty file

I'm trying to use less in my new project. I've installed dotLess and System.Web.Optimization.Less packeges (as described in Yet Another "Add LESS to your ASP.NET MVC Project" Post) and locally everything works fine. But when I publish project IIS7 server responds with empty css file /Content/Main/site?v=
Here is my web.config
...
<configSections>
<section name="dotless" type="dotless.Core.configuration.DotlessConfigurationSectionHandler, dotless.Core" />
</configSections>
<system.web>
<httpHandlers>
<add path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler, dotless.Core" />
</httpHandlers>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="dotless" path="*.LESS" verb="*" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition=""/>
</handlers>
</system.webServer>
<dotless minifyCss="false" cache="true" web="false" />
And my BundleConfig.cs
bundles.Add(new LessBundle("~/Content/Main/site").Include("~/Content/Main/Site.less"));
When I append css extension to my Site.less file (Site.less.css) server responds with content (/Content/Main/site?v=K-FFpFNtIXPUlQamnX3qHX_A5r7TM2xbAgcuEmpm3O41) but it still pure less with all its variables and etc.
The same with Bundle Transformer: Sass and SCSS
What have I missed?
As you are getting
/Content/Main/site?v=K-FFpFNtIXPUlQamnX3qHX_A5r7TM2xbAgcuEmpm3O41
then this means that the bundling is working correctly. Therefore there is probably something in Site.less that is causing the problem. One thing to watch out for is relative urls. For example if you have an image in your css
.bacon {
background-image: url(bacon.png);
}
The bundle will look for this with ~/Content/Main taken from the name of the bundle. (/site is fine and can be anything, it's just the name of the bundle)
The first thing to try is take out any imports, images etc and see if it bundles correctly. You can do this on your local dev build by enabling optimizations in the your bundle config.
BundleTable.EnableOptimizations = true
Another way to solve this is to remove dependency on System.Web.Optimization.Less and dotLess and use free Visual Studio Extension https://github.com/madskristensen/WebCompiler for precompilation of less to css (with and without minification).

ServiceStack and MVC4 not wiring up

I have an existing MVC 4 application. I wanted to add Service Stack to it. I tried installing the MVC host nuget package:
Install-Package ServiceStack.Host.Mvc
It installed 2 files in App_Start. I noticed I had to make a slight change because I was getting a build error:
In App_State/WebServiceExamples.cs, I had to update the interface references:
From: public class HelloService : Service
To: public class HelloService : ServiceStack.ServiceInterface.Service
I then went ahead and double checked the Web.config settings:
<location path="api">
<system.web>
<httpHandlers>
<add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
<!-- Required for IIS 7.0 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
</location>
I then ran the application and went to /api, I got a 404. From some further research I decided to manually update the endpoint via the apphost file:
SetConfig(new EndpointHostConfig
{
ServiceStackHandlerFactoryPath = "api",
});
This also didn't seem to work. What else am I missing?
Thanks for your time.
This should be in your web.config file as well:
<httpHandlers>
<add path="api*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
Also don't forget to remove the MVC '/api' route/path. You need to remove it so that ServiceStack and MVC don't compete for the '/api' route/path.
//REMOVE THIS FROM RouteConfig
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
);
If you want to be real explicit you can also add this to the RouteConfig
routes.IgnoreRoute ("api/{*pathInfo}");

Resources