We have a shared CSHTML view file with a javascript file include that many of our views use:
<script
src="@Url.Content("~/Scripts/customJavascript.js")"
type="text/javascript"></script>
We also need to register separate javascript files unique to individual CSHTML views. What is the best practice for doing this?
I haven't been able to find the equivelent of RegisterClientScriptBlock in MVC3 and we really can't put all javascript for all pages into one shared file.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by ricka6 on Nov 30, 2010 05:35 AM
I have checked this is working, Can you provide me a sample application?
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
I am getting the following error:
Unable to cast object of type 'ASP.index_cshtml' to type 'System.Web.IHttpHandler'.
That error is typically caused by ^F5 or F5 when you're in a view. What is the URL in the browser? You can't access the
/Views/Home/Index.cshtml file directly.
I don't believe I'm calling /views/home/index.cshtml directly.
I will work on recreating this issue in a smaller sample application.
I am also able to create the "Unable to cast object of type 'ASP.index_cshtml' to type 'System.Web.IHttpHandler'" error the following way using @RenderSection
I have also noticed this seems to be an unstable error. I can add the @RenderSection and run the app no problem.
Then as soon as I add the @section it errors out, I remove the @section and it still errors out so I have to remove the @RenderSection tag to get the application to run again and sometimes that doesn't even fix it and I have to revert back to a previous
version.
I noticed these same problems using the @Html.Script("~/Scripts/jquery-1.4.1.js", "jquery") method.
Unfortunately in the test MVC3 application I was able to include the javascript successfully with no errors using @RenderSection and @Html.Script
Here is the stack trace of the error:
at System.Web.WebPages.WebPageHttpHandler.CreateFromVirtualPath(String virtualPath, VirtualPathFactoryManager virtualPathFactoryManager) at System.Web.WebPages.WebPageRoute.DoPostResolveRequestCache(HttpContextBase context) at System.Web.WebPages.WebPageHttpModule.OnApplicationPostResolveRequestCache(Object
sender, EventArgs e) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
The test application I made did not give the same error and I really can't send you our enterprise application :)
However, it turns out that this is not a deal killing error. When I get exception, I just navigate back to the index page and it loads fine...go figure. Eventually the exception goes away all together.
So what I wound up using was @RenderSection which is like a ASP.NET Webform Placeholder, here's an example for anyone else trying to do this.
pfatcat
Member
14 Points
27 Posts
Separate javascript files with shared cshtml views in MVC3
Nov 24, 2010 03:03 PM|LINK
We have a shared CSHTML view file with a javascript file include that many of our views use:
<script src="@Url.Content("~/Scripts/customJavascript.js")" type="text/javascript"></script>
We also need to register separate javascript files unique to individual CSHTML views. What is the best practice for doing this?
I haven't been able to find the equivelent of RegisterClientScriptBlock in MVC3 and we really can't put all javascript for all pages into one shared file.
Thanks for the help...
mvc-3 razor javascript file
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Separate javascript files with shared cshtml views in MVC3
Nov 24, 2010 04:03 PM|LINK
This may help you,
public static MvcHtmlString Script(this HtmlHelper html, string path,string key)
{
ViewDataDictionary v=html.ViewContext.Controller.ViewData;
if(v["_script"+key]!=null)
return MvcHtmlString.Create("");
string f = "<script src=\"{0}\" type=\"text/javascript\"></script>";
v["_script"+key]=f;
return MvcHtmlString.Create(string.Format(f, VirtualPathUtility.ToAbsolute(path)));
}
and then try to put Html.Script inside your Views, PartialView, Layout and see the effect,
@Html.Script("~/Scripts/jquery-1.4.1.js", "jquery")
@Html.Script("~/Scripts/jquery-1.4.1.js", "jquery")
@Html.Script("~/Scripts/jquery-1.4.1.js", "jquery")
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
pfatcat
Member
14 Points
27 Posts
Re: Separate javascript files with shared cshtml views in MVC3
Nov 24, 2010 04:46 PM|LINK
Thanks very much for the help imran_ku07.
I created the public static MvcHtmlString Script method in a static helper class and implemented the @Html.Script in the child view.
I am getting the following error:
Unable to cast object of type 'ASP.index_cshtml' to type 'System.Web.IHttpHandler'.
Any ideas what might be causing this?
Thanks again...
pfatcat
Member
14 Points
27 Posts
Re: Separate javascript files with shared cshtml views in MVC3
Nov 24, 2010 05:03 PM|LINK
I wonder if it could be related to this issue? One poster mentioned he also got this error while trying to create a helper.
http://stackoverflow.com/questions/3921678/asp-net-mvc-3-beta-1-block-access-to-razor-views/4190326#4190326
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Separate javascript files with shared cshtml views in MVC3
Nov 25, 2010 02:05 AM|LINK
I have checked this is working, Can you provide me a sample application?
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Separate javascript files with shared cshtml views in MVC3
Nov 25, 2010 04:29 AM|LINK
That error is typically caused by ^F5 or F5 when you're in a view. What is the URL in the browser? You can't access the /Views/Home/Index.cshtml file directly.
pfatcat
Member
14 Points
27 Posts
Re: Separate javascript files with shared cshtml views in MVC3
Nov 29, 2010 02:06 PM|LINK
Thanks again for the help everyone.
I don't believe I'm calling /views/home/index.cshtml directly.
I will work on recreating this issue in a smaller sample application.
I am also able to create the "Unable to cast object of type 'ASP.index_cshtml' to type 'System.Web.IHttpHandler'" error the following way using @RenderSection
I have also noticed this seems to be an unstable error. I can add the @RenderSection and run the app no problem.
Then as soon as I add the @section it errors out, I remove the @section and it still errors out so I have to remove the @RenderSection tag to get the application to run again and sometimes that doesn't even fix it and I have to revert back to a previous version.
I noticed these same problems using the @Html.Script("~/Scripts/jquery-1.4.1.js", "jquery") method.
Views/Shared/_layout.cshtml
<head>
@RenderSection("Javascript", required: false)
</head>
Views/Group/index.cshtml
@section Javascript
{
This is a test...
}
pfatcat
Member
14 Points
27 Posts
Re: Separate javascript files with shared cshtml views in MVC3
Nov 29, 2010 02:28 PM|LINK
Unfortunately in the test MVC3 application I was able to include the javascript successfully with no errors using @RenderSection and @Html.Script
Here is the stack trace of the error:
at System.Web.WebPages.WebPageHttpHandler.CreateFromVirtualPath(String virtualPath, VirtualPathFactoryManager virtualPathFactoryManager) at System.Web.WebPages.WebPageRoute.DoPostResolveRequestCache(HttpContextBase context) at System.Web.WebPages.WebPageHttpModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Separate javascript files with shared cshtml views in MVC3
Nov 30, 2010 01:43 AM|LINK
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
pfatcat
Member
14 Points
27 Posts
Re: Separate javascript files with shared cshtml views in MVC3
Dec 01, 2010 01:42 PM|LINK
The test application I made did not give the same error and I really can't send you our enterprise application :)
However, it turns out that this is not a deal killing error. When I get exception, I just navigate back to the index page and it loads fine...go figure. Eventually the exception goes away all together.
So what I wound up using was @RenderSection which is like a ASP.NET Webform Placeholder, here's an example for anyone else trying to do this.
Master Page:
@RenderSection("Javascript", required: false)
Child Page:
@section Javascript{
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
}
Thanks everyone for the help.