In ASP.NET MVC 3, what class does Views and Partial Views inherit from? - view

In ASP.NET MVC 3, what class doe Views and Partial Views inherit from and can the class (presumably it's a class somewhere) be inherited from to extend functionality.
For example, the default LogOnPartial control has the following (using Razor syntax)
Welcome <b>#Context.User.Identity.Name</b>!
Where is the Context object exposed. What class makes that available to the partial view?

They inherit from System.Web.Mvc.WebViewPage. http://msdn.microsoft.com/en-us/library/system.web.mvc.webviewpage%28VS.98%29.aspx In older versions of razor it was required to add the #inherits but newer versions don't.

Related

How to override the built in templates in MVC Core similar in MVC 4

in ASP MVC (before MVC core like mvc 4,5 ) , we can override the built in templates like string , boolean from EditorTemplates/Boolean.ascx ,the question is :
Is there any way to do it in MVC Core similar to this article https://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html which is applied to MVC 2 ?
It still works the same way, though it was never .ascx. These are Razor views, so they'll need a .cshtml extension. Assuming you add a view like Views\Shared\EditorTemplates\Boolean.cshtml, then all you need to do is #Html.EditorFor(m => m.MyBooleanProp).

Why does Controller inherit from ControllerBase on GitHub but not in VS?

When pressing F12 (Go To Definition) on Controller in Visual Studio it shows you the abstract base class.
public abstract class Controller : IActionFilter, IFilterMetadata, IAsyncActionFilter, IDisposable
But when looking up Controller.cs on GitHub. It shows that it inherit from ControllerBase.
public abstract class Controller : ControllerBase, IActionFilter, IAsyncActionFilter, IDisposable
Why is this?
I am confused. Also, since HomeController inherit from Controller how can Controller inherit from ControllerBase when C# does not support multiple inheritance?
The two are from different points in time. So for example the file you linked to on GitHub is the current version of the file, note that it's from the Dev branch.
And more than likely you are not running code from the current dev branch.
If you click on the history button on GitHub for the file you linked to you will see the revision history for the file.
I checked the various version of this file by clicking on the <> button for each revision but non match the version of the code you are running. I see that the revision history for this file only goes back to Jan 22 2016. So prior to that the Controller code must have been defined in a different file or for some other reason the revision history was lost (Possibly when they renamed it from MVC 6 to Core MVC 1.0).
More than likely you are running code from RC1. That version of the Controller.cs on GitHub more closely matches what you are seeing: https://github.com/aspnet/Mvc/blob/6.0.0-rc1/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs It should match perfectly if it's the right version of the code but I see that it's still slightly different. It matches in the sense that it does not inherit from ControllerBase.
Also with regard to multiple inheritance. When HomeController inherits from Controller and Controller inherit from ControllerBase that's not multiple inheritance. Multiple inheritance would be if there were a ControllerBase and let's say a SomethingElse class and the Controller inherited from both of these classes in the controller definition. You are correct that C# does not support this.

How to layout base class and 3 extended classes in CodeIgniter?

This is the layout I'd like to use for controllers in CodeIgniter:
(base) editor
(extend) design
(extend) content
(extend) ...php
...where editor is a base class and design, content, etc. controllers extend editor. The editor class will have methods that are publicly accessible from the extended classes URL segments.
I've read some topics on here, and they recommended:
Library - this won't work as methods won't be publicly accessible (am I correct?)
Put base class in the same file as extended class and name the controller that - this won't work since I need to extend from multiple places.
Put all the files in the controllers folder, add require statements to each extended class - is this bad form?
I'm new to CI. What's the proper/correct way to handle this?
Thanks!
http://www.ellislab.com/codeigniter/user-guide/general/core_classes.html
extend the core CI controller with MY_Controller, then extend MY_Controller with your other controllers. all the other retain the functionality in MY and MY retains the functionality of the base controller
There is also this article which allows more controllers that don't have the MY_ prefix, which I use and find VERY useful!
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-base-Classes-Keeping-it-DRY

How do you add htmlHelpers into the Spark virew

I want to use the htmlHelpers in my spark view but I keep getting the following errors.
error CS0234: The type or namespace name 'Mvc' does not exist in the
namespace 'System.Web' (are you missing an assembly reference?)
I have added the System.Web.Mvc assembly into the project. I have also added the following code into the module (just for the sake of getting it working - I'll probably need to add this code to the bootstrapper --- not sure how to do that yet!)
var settings = new SparkSettings()
.SetDebug(true)
.SetAutomaticEncoding(true)
.AddAssembly("System.Web")
.AddAssembly("System.Web.Mvc")
.AddNamespace("System.Web.Mvc")
.AddNamespace("System.Web.Mvc.Html");
I also tried adding the namespace to a _global.spark file
Can someone tell me exactly what I must do to use the htmlHelpers in my spark view please.
The default Spark base view for Nancy doesn't include the public HtmlHelper Html { get; set; } property.
You can see the default view here.
The Spark view implemented for MVC integration is here, and you'll see the Html property exposed, which allows your Spark view to access it and invoke helpers.
In theory, you can inherit from NancySparkView, and specify that as your base view in your Spark settings, and add that property along with references to System.Web.Mvc etc in that class and your views should then be able to call into the helpers assuming everything is referenced correctly.
I'm not a Nancy expert, but I'm sure the type of the View is different than that of Asp.Net MVC. So, theoretically, you shouldn't be able to use MVC helpers, since they require the Html property on the View.

Why is CompareAttribute in the MVC namespace and not the DataAnnotations namspace?

MVC 3 includes a new validation attribute called CompareAttribute.
But why is this validation attribute in the mvc namespace and not with all of the other validation attributes in the DataAnnotations namespace?
Are the other validation attributes spread throughout other namespaces?
It's just the way things worked out. Derived types do not have to be in the same namespace as the base type. DataAnnotations is part of the .NET framework which releases a lot less frequently than a standalone project like ASP.NET MVC.
It's possible that there are types derived from ValidationAttribute in other namespaces, but most are part of the core DataAnnotations.

Resources