ASP.NET MVC Layout not working without URL routing - asp.net-mvc-3

I am facing strange behaviour for _Layout.cshtml
I have created a _Layout.cshtml for my project, it's working fine for Default rout i.e /Home/Index
then I have another controller named Biz wth action Categories, when I create the view for it using _Layout and run the project
all the content was displayed without any style and URL in the browser was localhost:54124/Biz/Categories.
when I configured rout in RouteConfig.cs file
as
routes.MapRoute(
name: "DT_FAP_Default",
url: "Categories",
defaults: new { controller = "Biz", action = "Categories", id = UrlParameter.Optional }
);
the layout is working fine
anybody have idea, what i am missing here?
I don't want to configure route for each action as my project has too many actions
Am facing same issue for other projects too. do I need any global configuration for vs?

finally, I found the solution
I changed the css linking code
Old:
<link id="layout-theme" rel="stylesheet" type="text/css" href="~/assets/themes/dark-blue.min.css" />
New:
<link id="layout-theme" rel="stylesheet" href="#(Url.Content("~/assets/themes/dark-blue.min.css"))" />

Related

MVC combined with WebAPI in ASP.NET Core, but where are the Views?

I'm trying to come to grips with the new ASP.NET Core 1.0 which states that both ASP.NET Web API and ASP.NET MVC are combined into one framework. So I decided to create a new ASP.NET Core Web API application using the ASP.NET Web API template from Visual Studio 2015.
But when I looked at the generated files/folders, there is no Views folder. The ValuesController is generated. I even added my own Controller which returns Web API responses. But I should also be able to add actions in my controller that returns, say, partial views as well right?
Since these two were merged, I assume I should be able to see a Views folder but I don't. How do I add the MVC parts to an app that was created using the Web Api template?
You can add a Views folder manually by following these steps:
Create a Views folder.
Create _ViewStart.cshtml in the Views folder with this content:
#{
Layout = "_Layout";
}
Create a Shared folder inside the Views folder.
Create _Layout.cshtml inside the Shared folder with this content:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>#ViewData["Title"] - Web App</title>
</head>
<body>
#RenderBody()
</body>
</html>

Error: The name 'Url' does not exist in the current context in asp.net mvc3

I added a MVC 3 View Page of type Razor to the root of the existing ASP.NET MVC 3 project.
ViewName: TestView.cshtml
I opened the above file and tried to add the following line of code in the view:
<head>
<link href="#Url.Content("~/Content/Styles/Themes/base/jquery.ui.all.css")" rel="Stylesheet" type="text/css" />
<title></title>
</head>
For the line above I am getting an error: The name 'Url' does not exist in the current context.
But when I am trying the same in a view within the Home or Account folder it is working fine for me.
Can any one help me to know what exactly I am missing?
This is because you have created the view page in the root of the MVC project rather than in the Views folder.
In the Views folder there is a web.config file which has the following section:
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
...
The namespaces specified here are used to compile the views which is why you get the 'Url does not exist' error message.
You could try to copy the contents of the web.config to the 'web.config' root but I'm not sure this is a good idea. And after doing so you may get a warning that there is no build provider registered but according to this link this will work perfectly fine.
I believe you may then have to register a new custom RazorViewEngine to specify the new paths to search for viewas the default view engine searches for views in theViews` folder.
I'm not sure if you may need to make any other changes but unless you need to have Views in the root folder, I would recommend you move your view to the Views folder to hopefully allow you to get it working.

Customize Layout templates In MVC3

I known There is a default _Layout.cshtml file in Asp.net MVC just like MasterPage in Asp.net Web Site. Can I define multiple layout templates in my MVC app? If so ,How to make it? Please give me some code example . Thanks.
You can create multiple Layout pages and use it in differnt views. You may mention the new layout pages inside your views by mentioning the Layout property in your views.
Create a new View and name it as _AdminLayout.cshtml under Views/Shared folder. You may create your HTML markup for your master page template there. Make sure you have #RenderBody() section in your Layout page so that the content of your normal views will be replaced here when it renders.
<html>
<head>
</head>
<body>
<h1>My new Layout</h1>
#RenderBody()
</body>
</html>
And if you want to use this Layout in a view, you can do it like this
#{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<p>The content here will be present inside the render body section of layout</p>
If you want to use the new Layout for all the Views by default without explicitly mentioning like the above code, you can do it in the Views/_ViewStart.cshtml file
#{
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

The resource cannot be found. ASP.NET MVC3

I have no idea why this error is occurring after debugging the project even though the codes are default.
Controller
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
View
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
Hi
</div>
</body>
Somehow or rather, after debugging, the Requested URL is always /Views/Home/Index.cshtml but accessing Home via browser is fine. (http://localhost:58323/home)
I googled and solution hints that the problem lies in global. But thats weird, I dont remember making any changes to it.
Global
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
);
}
Appreciate any help. Thank you
I think you just have your visual studio settings set so that the view is set to the start page. Right click on the project and go to properties, then to the web tab. Is the 'specific page' radio button selected with 'Views/Home/Index.cshtml' set as the value? Change it to use a start url. Personally I prefer not to have the debugger start up a browser and use Don't open a page.
Right click your web project -> Properties -> Web
check that you have the start action set to Specific Page with no value in the field.
My guess is that you have the start action set to current page.
This error might even cause if the hierarchy of the folder structure is incorrect in Views folder.
If you are adding views by right-click on Views folder.The new view added might be incorrectly placed in the folder hierarchy.
The way to troubleshoot the problem is:
Consider a view named index.ascx which is supposed to be linked to a Controller named HomeController.
Under the Views folder there should be a folder name Home (relating to the Controller) and the
index.ascx should reside in Home folder.
The best possible way to add view is to right-click just beside the public method which will show a Create View option in context-menu.If you crate view in such manner then folder hierarchy is automatically created.

using SquishIt in ASP.NET MVC 3

I'm trying to use SquishIt for minifying CSS and Javascripts in ASP.NET MVC 3 project.
When I use Render method:
.Render("~/content/themes/base/combined_#.css");
css is generated with random number instead of #, but link to css file is not generated and I need to insert it manually to cshtml file:
<link href="~/content/themes/base/combined_#.css" rel="stylesheet" type="text/css" />
but I don't know this random number, added to file name.
Without # it works fine.
But I have impression that Render should automatically generate css link according to this article:
http://www.codethinked.com/squishit-the-friendly-aspnet-javascript-and-css-squisher
Am I correct?
The following works great for me:
#Html.Raw(Bundle.JavaScript()
.Add("~/scripts/jquery-1.5.1.js")
.Add("~/scripts/jquery.unobtrusive-ajax.js")
.ForceRelease()
.Render("~/scripts/combined_#.js")
)
#Html.Raw(
Bundle.Css()
.Add("~/content/site.css")
.Add("~/content/site2.css")
.ForceRelease()
.Render("~/content/combined_#.css")
)
it emits:
<script type="text/javascript" src="/scripts/combined_B8A33BDE4B3C108D921DFA67034C4611.js"></script>
<link rel="stylesheet" type="text/css" href="/content/combined_97A4455A9F39EE7491ECD741AB4887B5.css" />
and when I navigate to the corresponding url the correct squished and combined resource is obtained.
There is also a Contrib project which provides a base Razor page to integrate SquishIt in an ASP.NET MVC application.
Or add a reference to the SquishIt.Mvc assembly and use the .MvcRender method
eg.
#using SquishIt.Framework
#using SquishIt.Mvc
#(Bundle.JavaScript()
.Add("~/scripts/jquery-1.5.1.js")
.Add("~/scripts/jquery.unobtrusive-ajax.js")
.ForceRelease()
.MvcRender("~/scripts/combined_#.js")
)

Resources