Style bundler rendering link incorrectly - css

This is a new MVC5 project, created from the blank ASP.NET template.
Here's my bundle registration:
Imports System.Web.Optimization
Public Class BundleConfig
Public Shared Sub RegisterBundles(Bundles As BundleCollection)
Bundles.Add(New StyleBundle("~/styles/main").Include("~/Styles/Layout.css"))
End Sub
End Class
...here's the relevant section of my view:
#Code
Me.Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
#Styles.Render("~/styles/main")
</head>
... and here's the HTML output:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link href="/styles/main" rel="stylesheet"/>
</head>
Note that the link's href should instead be:
<link href="/Styles/Layout.css" rel="stylesheet"/>
All of this of course results in a 404 error.
Things I've tried:
Followed the general guidance from the official documentation
Added a CssRewriteUrlTransform object, as indicated here
Both enabled and disabled Optimization, as indicated here
Updated the WebGrease package to the latest version
Checked Web.config for an umbracoReservedPaths key, as indicated here (none exists)
Added Bundles.IgnoreList.Clear() as indicated here
This exact construct is working correctly in another project, so obviously something's different. But I'm unable to spot it.
How can I get this bundle to render properly?

Related

Embedded Razor layout cannot be located

I'm using Razor with emails templates in .NET core 3.
While I can get this all to works, I'd like to change my templates file (called EmailLayout.cshtml and located at /Pages/Shared/) as an Embedded resource.
Whenever I do that in VS, I got this error:
InvalidOperationException: The layout view 'EmailLayout' could not be
located. The following locations were searched:
/Views/EmailLayout.cshtml /Views/Shared/EmailLayout.cshtml
/Pages/Shared/EmailLayout.cshtml
Why my file couldn't be found?
What should I add to get this to work?
Finally found the answer.
1) Install Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation nuget package
2) Add this to configuration in Startup :
services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
options.FileProviders.Clear();
options.FileProviders.Add(new EmbeddedFileProvider(appDirectory));
});
3) Change the service to services.AddRazorPages().AddRazorRuntimeCompilation();
Make sure that the Build Action of the file is set to Content in the properties of the View.
You could follow the steps to use EmailLayout.cshtml:
1.Create Layout View:
2.Be sure it is in /Pages/Shared folder:
3.EmailLayout should be like:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<h2>EmailLayout</h2>
<div>
#RenderBody()
</div>
</body>
</html>
4.Add the following code to your Razor Pages or _ViewStart.cshtml:
#{
Layout = "EmailLayout";
}

ASP.net vb code behinddeployed compiling error

Background: I am not an ASP developer, I understand OOP processes and can program in Javascript, Actionscript, and am learning PHP. I used to know VB.net in a limited fashion.
I am forced(by existing platform configuration) to develop a small .aspx page, i have been reading many tutorials, but am unable to get my page below to work. The best i can get is an 500 error message but no details are listed even though detailed error messages is configured. I dont have access to Visual Studios for this an am using Dreamweaver to develop.
I'm attempting to use the compiling method that compiles when the page is first viewed instead of precompiling
I cant seem to get this basic "hello world" type page to work. What am i doing wrong?
Edited code to represent changes made
CurrentNews.aspx
<%# Page Language="vb" AutoEventWireup="false" Src="/Scripts/CurrentNews.aspx.vb" Inherits="NewsFunctionality" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>News</title>
<link rel="stylesheet" type="text/css" href="CSS/NewsLayoutOne.css"/>
</head>
<body>
<div class="OuterDiv" runat="server">
<img id="NewsImage" src="Images/DefaultNews1.png" >
<div id="NewsBody" runat="server">Original Text</div>
</div>
</body>
</html>
CurrentNews.aspc.vb
' VB Document
Class NewsFunctionality
Inherits System.Web.UI.Page
'On load event for the page linked to this class file
protected sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
NewsBody.InnerHTML = "Dynamic Text Generated."
End Sub
End Class
From a quick glance, i would say at minimum your code would not do what you expect. You are putting "<P>Dynamic Text Generated</P>" to be rendered in an input field.
Other than that though im not seeing anything that obviously would not work. What is the behavior you see?
Your Page_Load sub should declared protected. The relation between aspx page and aspx.vb class is somehow like inheritance.

ASP.NET MVC 3 send <head> before <body> renders

I have asp.net mvc application, that uses razor view engine.
I want to send head to browser, before body renders, to start parallel loading css, js and images, linked in css. (You can see, how this technique works on SO in chrome developer tools - network for example)
I found question about it in asp.net web forms: Send head before body to load CSS and JS asap
I tried to use this solution, but it don't work.
For razor engine next sequense of steps is actual:
action returns view result
_ViewStart.cshtml executes (set ViewBag.Layout)
view executes from first line to last (with code inclusions and sections)
view engine checks ViewBag.Layout and if it found - executes Layout (with renderind body and sections)
I think that good solution is to divide step 3 into 3 parts:
generating content for Head and Css section
send to browser
generating other part of view
Other solution is to static include all.css and basic .js files in (without sections content, generated from view), send head, and then generate view (with generation FooterScript section).
In both ways I need to start execution from Layout page, not from view. For first: Layout (head) - view (sections) - layout (flush) - view (other) - layout (body). For second: Layout (head + flush) - view (all) + Layout (body).
My _Layout.cshtml file:
<html #Html.Raw(ViewBag.xmlns)>
<head>
<title>#ViewBag.Title</title>
#Html.Partial("_MetaTags")
<link href="#Url.ThemeCss("jquery-ui-1.8.18.custom.css")" rel="stylesheet" type="text/css" />
<link href="#Url.Css("masterPage.css")" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="/favicon.ico"/>
#RenderSection("Css", required: false)
<script src="#Url.CommonScript("jquery.common.min.js")" type="text/javascript"></script>
<script src="#Url.Script("Master.js")" type="text/javascript"></script>
#RenderSection("Head", required: false)
</head>
<body>
<div id="pageWhithoutFooter">
<div id="main">
#RenderBody()
</div>
</div>
#RenderSection("FooterScript", required: false)
</body>
</html>
Howto?
Try putting your head section into a Partial view then call this in your controller:
PartialView("_PageHeader").ExecuteResult(ControllerContext);
Response.Flush();
// Generate model
return View(model);
Not tested this but I can't see why it wouldn't work.

ASP.NET MVC 3: ViewModel for master template?

When I created my first empty MVC 3 project, this is the /Views/Shared/_Layout.cshtml file's contents:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>#ViewBag.Title</title>
<link href="#Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="#Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
</head>
<body>
#RenderBody()
</body>
</html>
The first thing I notice is that the <title>#ViewBag.Title</title>. The last thing I want to do is be using a dynamic "ViewBag" instead of a strongly-typed ViewModel.
How do I change my _Layout.cshtml so that the master template uses a strongly-typed ViewModel instead?
You can easily use #model to define the model type, but who will set the master pages's model or create it? The controller handles its own view and the model for the master page will be different based on the model created in each controller.
So, you may want to make all the models you pass to the views inherit from a base model class that has the properties required for master page and make the master page have the base type as the model type, but it'll be too ugly.
I'd suggest live with it for the simple cases like title here, if you find that you do a lot of stuff in there, have your own controller, action and view that perform the shared parts, and call Html.RenderAction() in the master page to get that executed.
http://gurustop.net

Cannot call external javascript function from SITE.MASTER

I have a Site.Master in my ASP.NET project which defines a HEAD section as follows
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title id="MasterTitle">MyApp</title>
<link rel="icon" href="Content/icon.ico" type="image/x-icon" />
<link href="Content/mycss.css" rel="stylesheet" type="text/css" />
<script src="Content/mycode.js" type="text/javascript"></script>
</head>
In the mycode.js file, I have a function called GetSels();
function GetSels()
{
//do stuff
}
If the GetSels function is defined in Site.Master, GetSels is callable. If it's in mycode.js, it's not.
Every code example I've seen seems to say this should work.
What am I doing wrong?
This should really work perfectly as I have done this multiple times myself.
Check that the code in your external javascript file runs correctly on page load, this is just to make sure that it is indeed being loaded correctly into your document. For example set an alert("It's loaded"); in your external .js file.
have you checked that your reference to mycode.js is correct? if your using a relative path try "~/Content/mycode.js" in your reference.

Resources