MVC4, two different masterpage - asp.net

How can I use two different master page or layout in my web project with mvc4?

You create 2 different Layouts and then inside your view you can choose which layout to use:
#{
Layout = "~/Views/Shared/MyLayout.cshtml";
}
This can also be done inside the controller action rendering the view:
return View("Index", "MyLayout", myViewModel);

I don't claim to be an expert in MVC. As a matter of fact, I just started using it.
This is what I did. I copied Site.Master using Windows Explorer, renamed it, added it to Shared Views, edited it, and started using it. Worked for me.

Related

Sitecore controller rendering causing StackOverflowException

I am trying to do a simple controller rendering with Sitecore 8 and for some reason it's producing a StackOverflowException on the line within the main layout markup that contains the reference to the placeholder it is to be rendered in. This seems to crash the worker process, but you can see the stack overflow on debugging the process:
Here is my very basic controller:
And here is my controller rendering definition:
Reproduction notes:
This is occurring in a vanilla Sitecore 8 installation (rev. 150427 -installed via SIM).
The MVC project is also vanilla -created with empty ASP.NET project, then NuGetting in MVC 5.1.
Web.config & Global added to project from the Sitecore site root in wwwroot.
FYI - everything is absolutely fine doing a view rendering - it's just controller renderings that seem to be causing a problem
So the problem was actually pretty simple in the end.
Returning a ViewResult when the view is intended as a partial view (which all Sitecore renderings will be) then you must set the layout property in the markup to null:
#{
Layout = null;
}
Otherwise MVC will try to wrap the layout file around it, which of course contains your Sitecore placeholder, which causes an infinite loop and crashes the worker process with a StackOverflowException.
So in the context of Sitecore, either return a PartialViewResult or return a ViewResult with the layout set as null.
I guess there is something missing in placeholder setting, could you check in path sitecore/layout/placeholder setting?
There should be a placeholder key which you are trying to use.
Hope this will help
Cheers!!
I think the issue can be with method View() being called without any parameters which can cause re-rendering the whole Sitecore page again.
Try to add parameter to View() like that:
return View("/Views/Courses/Index.cshtml");
Or whatever is the path of the view you want to return.
EDIT:
As #David Masters found, for some reason the issue is with calling View instead of PartialView method with the full path as a parameter. The correct code is:
return PartialView("/Views/Courses/Index.cshtml");

Reusing ASP.NET code in multiple pages

Is there a way to reuse a piece of ASP.NET code (that tag-based code, not the code-behind) in many different pages? For example I have a <div> with some contents in it that appears in 5 different pages of my site. I'm looking for a technique through which I could save this <div> in a separate web-content file so to speak, with maybe a different extension like MyDiv.ASPC and could then do a server-side tag anywhere in a webpage like:
<asp:Import href="~/MyDiv.aspc" />
and ASP.NET would inject the entire content of this file at that point where this tag appears.
I thought of creating a user control for this, but I'm not sure if a user control always injects precisely what is written in its body, or could there sometimes be unwanted tags generated by user control itself.
Or are there existing better ways of doing this?
Edit
About Master Pages, they are far away from what I'm looking for. They are actually good for a common basic layout of your website. My idea is quite opposite of that. My pages do not have a common layout; it is just that they have one common <div>. It is more closely fulfilled by a UserControl.
For UCs, my fear is that they generate more than what is written in their body, whereas what I'm after is a simple text injection. To put it technically, what I'm looking for is basically a preprocessor step (kind of #include thing in C++) rather than a compiler step, if you see what I mean.
You need to use ASP.NET User Controls, as these are specifically created to be the solution to the problem you are describing. For more information, see MS Documentation.
From their documentation...
In addition to using Web server controls in your ASP.NET Web pages,
you can create your own custom, reusable controls using the same
techniques you use for creating ASP.NET Web pages. These controls are
called user controls.
A user control is a kind of composite control that works much like an
ASP.NET Web page—you can add existing Web server controls and markup
to a user control, and define properties and methods for the control.
You can then embed them in ASP.NET Web pages, where they act as a
unit.
An empty userControl would do just that - nothing. A user Control just adds it's contents to the page, or usercontrol hosting it. It adds nothing extra.
UserControls give you a nice easy page fragment type approach to reusing content. They work great within a project & most people use them for just that.
If you wanted to make something more reusable across projects, you could write server control. It's more involved, but much more reusable. Google should be able to find you many tutorials on how to do this.
Ran a short test. User Controls do not enter extra tags as long as you don't place any Runat="Server" tags in it, so this would indeed be a solution I guess.
You can also read output from a cache object where you would read your files.
So
<%= Static.ContentXyz %>
would mean:
public static class Static
{
public static string ContentXyz
{
get
{
string s;
if (!this.cacheDictionary.TryGetValue("ContentXyz", out s))
{
s = File.ReadAllText(Server.MapPath("ContentXyz.html"));
this.cacheDictionary("ContentXyz", s);
}
return s;
}
}
}

I want to make the asp.net MVC page configurable, any ideas?

guys, I was applied a big task is about to make the MVC page configurable.
It means:
It just like the webpart in webform.
1.We can config the partial view in the view,make the partial view enable or not,we can drag and drop the partial view anywhere in the container.
2.One partial view is related to a simple mode(entity),and also the fields of the form in the partial view can be configurable:enable or not and the position can be adjusted.
now I have some ideas as following:
1.I create an model base, let other models inherit from model base,model base just hold the metadata related to the fields and the model itself.
2.Render the settings which is in model base to hidden field throght html helper and partial view.And on the client, I just use the jquery to handle the layout according to the setting in the hidden field.
3.The partial view I just let it to render only, and the real logic I will hand it on the page which contain the partial view. I think this would be simple and extendable.right?
So any ideas here? I really think this task is complicate.
In the past, Omar AL Zabir Blog has an portal website, called dropthings, maybe it is closed now. He tried to implemented the things like igoogle did. You can see that project at codeplex.
And you also can find some ideas for that find of application at Lakkakula's Blog
Hope this help.

Strange Problem with asp.net website

I've built an asp website and i have the following issues:-
i'm using a master page in it and have defined two contentplaceholders one in head one in content, and i've specified the page title in the top most directive at the #page directive but the page title doesn't show up. I have to manually add a tag for it.
Secondly when i create a content page from a master page it creates it and when i rename it, it doesn't rename it's class. It remains _Default, thus every page was having an inherit to _default.
Most importantly
I'm using a page to enter and view data to the database. I've used a boolean called isadmin which i set according to credentials at page load. and i'm added a panel where it's visible property is set to Visible = '<%#IsAdmin %>'. It works properly when i run it through the visual studio environment but when i publish it and run it doesn't work and the panel just comes and stays there. Why is it happening? Any idea?
Thanks
The Visible problem is fixed as i had to enable windows authentication on the server. Awaiting answers for the other two issues. Thanks
Try to add a <head runat=server> to the master page. Only then ASP.net can "see" the tag and modify it
It is not that bad that several aspx-pages have identical class names. ASP.NET 2.0 started to process every page as its own compiling unit or so. Pages cannot see each other. There is a special directive to make pages see each other and instanciate or manipulate them. So it should not do much harm
Maybe you did not test this correctly and are mistaken that it DOES work in Visual Studio??? But in any case I would suggest that you move your logic into OnInit, then it runs much earlier. I think the control tree is build before Page.OnLoad. What you do is data binding, that might run only if DataBind is called, I'm not sure
Or use the safe way: Make IsAdmin a property so that it initializes itself on first call and caches the result in a variable
Regarding question #2 - add your content pages via Project -> Add New Item, and name it appropriately there. That way the naming is consistent and correct throughout.
Regarding #3, what HTML is output when you run it from the server?
your first issue can be solved by filling out the title part of the #Page directive in your .aspx pages. The master page will display that text in the browsers title bar.
and prob #2 should be solved by adding the files using the file add option in visual studio.

Automated testing for Classic ASP

Does anyone do automated QA testing for a Classic ASP site? I started looking at WatIn and MBUnit but not sure of the best way to structer the tests.
The new WatiN 2.0 beta 1 does offer some base classes to help you structure test classes.
It basically comes down to having a class for each page (inheriting the WatiN.Core.Page class). In these page classes you add properties for each control you want to access. Something like:
public Button OkButton
{
get { return Document.Button("okbuttonId");
}
and you can create methods to wrap some more complicated actions an a page. For instance:
public void AddPerson(string name, string email)
{
/// logic goes here tp click on NewButton, set the textfields and click on OkButton
}
These page classes offer the advantage of defining your elements in one place.
In your test code you can create a page class as follows:
using(var ie = new IE("www.somedomain.com/person"))
{
var page = ie.Page<PersonDetailPage>();
page.AddPerson("J. Doe", "jdoe#example.com");
// Do some Assert
}
Another interesting base class to help you structure your code is the Control class. When you use ASP you'll use controls which will not render to just one html element in the rendered page. Instead it will often be a construct of elements contained in a Div element. When creating your own control class and inherit Control you'll be able to wrap the controls (html) internals and behavior. This makes it very easy to reuse the control in your page classes. Following an example on how to instantiate a control:
var calendar = Document.Control<CalendarControl>("calendarId");
Hope this gives you some insight in how you can structure your Pages and controls.
Jeroen
FWIW, we have been using WatiN and MbUnit for web integration testing for the past 3 years.
We have separated the tests into 3 projects:
QA.Framework: Contains glue code for setting up test fixtures and various custom MbUnit and WatiN extension.
QA.SiteMap: Contains Page and Control classes arranged hierarchically into namespaces that correspond to different domains and parts of the sites. This project serves to decouple tests from the major part of the web site structure. You can think of it as a model of the sites.
QA.Tests: Contains the actual tests also arranged hierarchically into namespaces. Tests leverage the SiteMap and Framework as needed to interact with the web site. This way there is much less code duplication than if each test contained the same button ids over and over...
Jeff.
I tested ASP site with Watir. If you are looking for a way to structure tests, take a look at WatirCraft framework.

Resources