Saturday, 15 March 2014

c# - Custom UrlHelper method not being included -



c# - Custom UrlHelper method not being included -

i made custom helper trying include available pages.

helper:

namespace project.cachebreaker { public static class cachebreaker { public static void cachebreak( urlhelper url, string contentpath) { url.content(contentpath); } } }

i tried expose web.config:

<system.web> <pages> <namespaces> <add namespace="project.cachebreaker"/> </namespaces> </pages> </system.web>

i got error while trying access in view:

<script src="@url.cachebreak("~/scripts/link.js")" type="text/javascript"></script>

which stated:

"description: error occurred during compilation of resource required service request. please review next specific error details , modify source code appropriately." "compiler error message: cs1061: 'system.web.mvc.urlhelper' not contain definition 'cachebreak' , no extension method 'cachebreak' accepting first argument of type 'system.web.mvc.urlhelper' found (are missing using directive or assembly reference?)"

i thought had taken necessary steps, why isn't custom method available?

but urlhelper doesn't homecoming anything. it's void. can't perchance phone call with: @url.cachebreak(...). if want helper used way should homecoming ihtmlstring or string:

public static string cachebreak(this urlhelper url, string contentpath) { homecoming url.content(contentpath); }

oh , way having helper wraps around url.content seems kinda useless, coz straight write:

<script src="@url.content("~/scripts/link.js")" type="text/javascript"></script>

also wbe.config talking in question seems wrong wbe.config. know, there's ~/web.config , there's ~/views/web.config 2 different files. if using razor view engine should define namespace in ~/views/web.config file:

<system.web.webpages.razor> <host factorytype="system.web.mvc.mvcwebrazorhostfactory, system.web.mvc, version=4.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.optimization"/> <add namespace="system.web.routing" /> <add namespace="project.cachebreaker"/> </namespaces> </pages> </system.web.webpages.razor>

c# asp.net-mvc-3 extension-methods

No comments:

Post a Comment