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.
Related
How do I add a selfhosted Asp.Net MVC 5 application inside my existing Winforms project? I already have the self-hosted mvc 5 app working fine as a separate console app.
But,now I need to load this MVC 5 project into a subfolder of my existing Winforms application and I need make calls to my existing business logic classes inside the controller.
On the web about adding MVC support to an existing WebForms application. The two play nicely side-by-side in the same project
First you would need to make your MVC project a portable area. Details about this can be found here. A Portable Area is a dll that contains items that would normally be part of your solution. Portable Areas contain Views, Controllers, Models, even JS Scripts, CSS files and images.
Disadvantages of a portable Area:
Every time you make a change to any View, JS File, CSS File, or image within your Portable Area, you will need to rebuild it. I emphasize these components because they do not normally need to be rebuilt when being tested or developed.
This can become a problem. If you find yourself re-building every time you tweak CSS, 30 second changes become 2 minute changes. Make 30 of those and you've stretched 15 minutes worth of work into 2 hours.
Next, you need to update your web.config file. Replace the default compilation section with the following:
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
Also add a pages element with the namespaces shown:
<pages>
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
</namespaces>
</pages>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
and add the MVC HTTP handler and a binding redirect for MVC:
<httpHandlers>
<add verb="*" path="*.mvc"
validate="false" type="System.Web.Mvc.MvcHttpHandler,
System.Web.Mvc, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/>
</httpHandlers>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Now in our Global.asax file we need to register the portable area:
public class SomeHybrid: System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
Now, at this point I can visit both pages. The WebForms page is a file on disk, so ASP.NET routing passes requests directly on to this page when I /default.aspx. The ASP.NET Routing engine is engaged so I can also hit /Home/Index.
If I want to get fancy, You can add a PageRoute so You have pretty URLs when visit my WebForms pages as well. Just add a route in the Global.asax like this. Make sure that simple routes like these come first, as the default ASP.NET MVC route is very "greedy" and would gobble up a simple URL like /calculator
routes.MapPageRoute("WebFormThing", "Calculator", "~/Default.aspx");
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
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.
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).
I add that to my header <%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> and am able to access ViewData and all its internals as well as all the mvc objects like url and html. As soon as I add "System.Web.Mvc.ViewPage<app.Models.tTable>" I have no access to any mvc classes and helper methods. I am confused on why this is. I have done an upgrade from mvc preview 5 to rc1 recently but my transition to rc1 was flawless without any errors or bugs.
After fishing (alot of fishing) around I found the answer, this is because I didn't read the official release notes rather ran through a tutorial from a blog but anywho to get this working you need to replace the <pages> node in the web.config file in your "/Views" directory with
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
I added the commenting from a pre built web.config file
There's a trick. There's a web.config under the /Views folder. Wow, I didn't see that.
Thanks for posting