Custom Html Helpers in Asp.net MVC Areas - asp.net-mvc-3

I have some custom Html helpers for my Asp.net MVC 3 app. In the main application they work correctly as I have put the following in my Web.Config:
<pages clientIDMode="AutoID">
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
<add namespace="WebDibaelsaMVC.Utils.HtmlHelpers" />
<add namespace="WebDibaelsaMVC.Utils.HtmlHelpers.DTOs" />
</namespaces>
</pages>
but I have now created an area and to make my custom helpers work I have to add a using in every page where I use them. Is there a way to add the default namespaces for that area?

If you are using Razor you might need to add the reference to the <namespaces> section in the ~/Views/web.config and ~/Areas/YourAreaName/Views/web.config and not the main ~/web.config file. Also make sure you recompile the project, open close the view, maybe even restart Visual Studio for changes to take effect (in terms of Intellisense, it will work if you run the project).

Related

asp.net 4 Webform application with custom urlrewrite fails on IIS 8

We had some ASP.NET 3.5 and ASP.NET 4 webform websites with Custom UrlRewrite which programmatically map SEO friendly urls (http://example.com/key/This-is-sample-text-in-url.html) to physical address (http://example.com/key.aspx)
These websites worked perfect in IIS 6 with C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll for 3.5 and c:\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll for 4.0 in application extension mapping window.
after migrating to windows 2012 all 3.5 websites work using classic .NET 3.5 AppPool and this statement in handlers section:
<remove name="ASP.Net-ISAPI-Wildcard" />
<add name="ASP.Net-ISAPI-Wildcard" path="*"
verb="*" type="" modules="IsapiModule"
scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll"
resourceType="Unspecified"
requireAccess="None"
allowPathInfo="false"
preCondition="classicMode,runtimeVersionv2.0,bitness64"
responseBufferLimit="4194304" />
I expected using the same Classic .NET 4.0 AppPool and the following handler should work but didn't:
<remove name="ASP.Net-ISAPI-Wildcard" />
<add name="ASP.Net-ISAPI-Wildcard" path="*"
verb="*" type="" modules="IsapiModule"
scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"
resourceType="Unspecified"
requireAccess="None"
allowPathInfo="false"
preCondition="classicMode,runtimeVersionv4.0,bitness64"
responseBufferLimit="4194304" />
I still get HTTP Error 404.0 - Not Found
Module: IIS Web Core
Notification: MapRequestHandler
Handler: StaticFile
Error Code: 0x80070002
I cant downgrade website and couldn't find anything useful to find the source of error. Site log file do not provide any details, no errors in event logs.
I suspected there may be a conflict in handler mappings and tried to disable irrelevant or suspicious entries via Web.Config but there was no change.
I don't know but maybe ASP.Net-ISAPI-Wildcard is not able to catch the request and finally process ends up with StaticFile handler, though in ordered list, ASP.Net-ISAPI-Wildcard is on top and StaticFile is the last.
I appreciate if anybody has a solution to this.
p.s. in the sample above target page works i.e. url to (http://example.com/key.aspx)
Solved
This is in case anybody face the same situation
The handler I added was correct, but only for 64bit applications, so when I added 32 bit version of handler problem solved. As I had set Any Platform in configuration Manager I didn't think this may be a platform matter.
<remove name="ASP.Net-ISAPI-Wildcard" />
<add name="ASP.Net-ISAPI-Wildcard" path="*"
verb="*" type="" modules="IsapiModule"
scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"
resourceType="Unspecified"
requireAccess="None"
allowPathInfo="false"
preCondition="classicMode,runtimeVersionv4.0,bitness64"
responseBufferLimit="4194304" />
<remove name="ASP.Net-ISAPI-Wildcard-32" />
<add name="ASP.Net-ISAPI-Wildcard-32" path="*"
verb="*" type="" modules="IsapiModule"
scriptProcessor="C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll"
resourceType="Unspecified"
requireAccess="None"
allowPathInfo="false"
preCondition="classicMode,runtimeVersionv4.0,bitness32"
responseBufferLimit="4194304" />

How to add Kendo UI HTML helpers to my ASP.net MVC 4 project?

Simple question: I've installed Telerik DevCraft Ultimate on my workstation. I've looked at the examples showing the use of the Kendo UI HTML helpers.
What reference do I add to my project to be able to use them?
Configure your ASP.NET MVC layout page to include the Kendo UI Web JavaScript and CSS files:
<link rel="stylesheet" href="#Url.Content("~/Content/kendo.common.min.css")">
<link rel="stylesheet" href="#Url.Content("~/Content/kendo.default.min.css")">
<script src="#Url.Content("~/Scripts/jquery.min.js")"></script>
<script src="#Url.Content("~/Scripts/kendo.web.min.js")"></script>
<script src="#Url.Content("~/Scripts/kendo.aspnetmvc.min.js")"></script>
Add a reference to the Kendo.Mvc.UI namespace to your web.config. Then the Kendo HtmlHelper extension would be availble in your views. Rebuild your project after adding the namespace to the web.config (required for Visual Studio to show intellisense for Kendo.Mvc.UI).
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="Kendo.Mvc.UI" />
</namespaces>
</pages>
</system.web.webPages.razor>
Use any Kendo UI HtmlHelper extension
#(Html.Kendo().DatePicker().Name("Birthday"))
more details at http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/introduction

ASP.NET MVC stops executing actions when runAllManagedModulesForAllRequests=false

When I set runAllManagedModulesForAllRequests=false to increase MVC performance the MVC stops executing Actions like [ActionName("membership.asp")]. I get 404 error on IIS7.5.
Any idea how to solve this?
I have found the solution by myself. Just need to add extension to web.config file which should be handled by MVC.
<system.webServer>
<modules runAllManagedModulesForAllRequests="false" />
<handlers>
<add name="ClassicASP" path="*.asp" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="File" preCondition="integratedMode" />
</handlers>

Azure and HttpHandlers

I have a HttpHandler for an asp.net mvc application. I've tested the handler for asp.net and asp.net mvc 3 applications and everything works as expected.
When I use the HttpHandler in an Azure based asp.net mvc 3 application the 'ProcessRequest' method is NOT being called - I can see the HttpHandler being created.
I have the following web.config and this works for a standard asp.net mvc 3 app:
<system.web>
<httpHandlers>
<add type="TestWebRole.Infrastructure.HttpHandlers.EPubHandler"
path="*.epub"
verb="*" />
</httpHandlers>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<add name="EPubHandler"
type="TestWebRole.Infrastructure.HttpHandlers.EPubHandler"
path="*.epub"
verb="*"
resourceType="Unspecified"
allowPathInfo="false"
modules="IsapiModule"
scriptProcessor="%path%\aspnet_isapi.dll"/>
</handlers>
</system.webServer>
I also have the following statement in the global.asax.cs file to specify ignoring asp.net ,mvc routing for the extension type:
routes.IgnoreRoute("{resource}.epub/{*pathInfo}");
What do I have to configure to get this working when running in Azure - locally or deployed into the cloud?
Just to try, why don't you remove the httpHandlers section under system.web and leave only that in the system.webServer. And also strip all the unnecessary attributes from the one under system.webServer (scriptProcessor, modules, allowPathInfo).
And also you may check for any uncought exception, event log entry, anything showing some kind of error.

How to enable Intellisense in Html Helper with MVC3?

I basically followed this article: http://develoq.net/2011/how-to-create-custom-html-helpers-for-asp-net-mvc-3-and-razor-view-engine/
My HtmlHelper class is located in the root directory of my MVC project:
using System.Web.Mvc;
namespace MvcHtmlHelpers
{
public static class HtmlHelperExtensions
{
public static MvcHtmlString Hello(this HtmlHelper helper)
{
return new MvcHtmlString("Hello!");
}
}
}
If I reference it in a view: #using MvcHtmlHelpers; I get intellisense and view code validates fine (no red underlines).
If I don't reference it in a View, but reference it in either one or both of the 2 web.config files, I don't get intellisense and #Html.Hello() is now red underlined.
Now the best part is that when I run, it renders perfectly fine even if I don't have it referenced anywhere at all. Ideally, I would like to reference it once in the web.config and get Intellisense in a view without referencing it in that view.
UPDATE: I guess something got cached somewhere. It does NOT render when it's not referenced anywhere. I'm sorry. I would still like to get Intellisense and validation in a view.
If you are using Razor view engine you need to reference it in ~/Views/web.config and not in ~/web.config:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="MvcHtmlHelpers" />
</namespaces>
</pages>
</system.web.webPages.razor>
Then recompile, close and reopen the Razor view and if it is your lucky day you might even get Intellisense. If not you may curse at Microsoft.

Resources