HtmlHelper class in System.Web.Webpages.Html vs System.Web.Mvc namespaces - asp.net

In ASP.NET MVC3 I'm trying to set the css class that the validation error helper method sets. (Not this question, the accepted answer just ADDS the class, I want to override it completely.) In looking at the MVC3 source I've found the ValidationInputCssClassName property on the HtmlHelper class. It is settable and it stores the value in a storage provider if set. The get method for the property returns [the provider value] ?? [the default class name].
Now if you just type in HtmlHelper.ValidationInputCssClassName (MSDN entry) in your controller code you'll see that it is a static readonly field. The reason for this is that there are two HtmlHelper classes, one is in the System.Web.Mvc namespace, and the other is in the System.Web.Webpages.Html namespace. The property System.Web.Webpages.Html.HtmlHelper.ValidationInputCssClassName (MSDN entry) can be set, but it doesn't seem to have any effect the generated code no matter where I set it.
What am I missing? And what is the difference between these classes?

Looking at the namespace description, it appears that one of them is designed for Razor and is intended to be used with WebMatrix.
Reference: http://msdn.microsoft.com/en-us/library/gg549171(v=vs.99).aspx
Also:
The System.Web.Mvc namespace contains classes that are used to create HTML elements.
The types in this namespace are in the System.Web.WebPages assembly and are identical to the equivalent types in the System.Web.Mvc assembly.
Reference: http://msdn.microsoft.com/en-us/library/system.web.mvc(v=vs.99).aspx

I'm not positive but I think System.Web.WebPages.Html is for using ASP.net WebForms with Razor.
Since ValidationInputCssClassName, etc. are all readonly fields, I think the only way you are going to get around this is by creating your own HtmlHelper extension methods so you can customize this behavior.
From MSDN:
The System.Web.WebPages namespace contains core classes that are used
to render and execute pages that are built using ASP.NET Web Pages
with the Razor syntax.

Related

RenderPartial does not exist in System.Web.Mvc.Html but RenderPartialExtensions does in cshtml

I want to embed other cshtml views into my main view. After doing some research I found that Html.RenderPartial can be used for this purpose. But I'm not seeing RenderPartial and only getting RenderPartialExtensions.
I'm using System.Web version 4.0.0.0 and VS2017.
Also, installed Microsoft.AspNet.Mvc v5.2.5 but that didn't help either.
If its not possible, can you give an example of how to use RenderPartialExtenstions in cshtml?
RenderPartialExtensions adds the RenderPartial extension method to the HtmlHelper class (which is named "Html" in ASP.NET MVC views), so using RenderPartial is using RenderPartialExtensions by definition.
Here's a typical use of RenderPartial:
#{
Html.RenderPartial("~/Views/Shared/_MyPartialView.cshtml");
}

ASP.NET CodeFileBaseClass attribute vs. inherit from System.Web.UI.Page

I've just created a base class for my pages by inheriting from System.Web.UI.Page:
public abstract class PageBase : System.Web.UI.Page
{
...
}
When I noticed that you can also declare a base page in an ASP.NET view:
<%# Page Language="C#" CodeFileBaseClass="PageBase.cs" CodeFile="page.aspx.cs"
Inherits="page" %>
Can someone explain what the pros and cons of either method are? When would you use one over the other, or are they both the same? What happens if you used both at the same time?
CodeFileBaseClass, CodeFile, Inherits work together with inheritance, not in place of inheritance.
For example, specifying CodeFile="page.aspx.cs" without page.aspx.cs existing will result in:
Parser Error Message: The file '/page.aspx.cs' does not exist.
Assuming page.aspx.cs exists, specifying CodeFileBaseClass="PageBase.cs" without PageBase.cs existing will result in:
Parser Error Message: Could not load type 'PageBase.cs'.
On the other hand you may inherit from PageBase without specifying the CodeFileBaseClass attribute. This however could result in possible unexpected behaviour when referencing controls on the page from the base class.
To quote from Microsoft's #Page MSDN Documentation:
CodeFileBaseClass
Specifies the type name of a base class for a page and its associated code-behind class. This attribute is optional, but when it is used the
CodeFile attribute must also be present. Use this attribute when you want to implement a shared scenario, where you define common
fields (and optionally, associated events) in a base class to
reference the controls declared in a Web page. Because of the ASP.NET
code generation model, if you defined the fields in a base class
without using this attribute, at compile time new member definitions
would be generated for the controls declared in the Web page (within a
separate partial class stub), and your desired scenario would not
work. But if you use the CodeFileBaseClass attribute to associate
the base class with the page, and you make your partial class (its
name is assigned to the Inherits attribute and its source file is
referenced by the CodeFile attribute) inherit from the base class,
then the fields in the base class will be able to reference the
controls on the page after code generation.

asp.net access user control in vb class

i have a usercontrol named filebox (which wraps a fileupload and some related stuff)
in a certain .net class i have need to use this type as a ctype
but i cant seem to call ctype(aobject,filebox)
or either ctype(aobject,Controls_filebox) (which is the correct name)
how can i convert an object to this type?
thanks a million!
You need to add the class' namespace.
It's probably in the ASP namespace; you can check in the Object Browser.
You can change the namespace by adding Class="My.Namespace.Filebox" in the <%# Control directive.

All the data annotations (or attributes) availabe in Asp.net M-V-C

Is there any way to look at all the data annotations or attributes available in Asp.net MVC?
i.e. for validation we have "Required", "StringLength" etc, for Action verbs, "HttpPost", "HttpGet" etc, similarly "Bind", "MetadataType" etc.
I am kind of new to Asp.net MVC and MVC is loaded with attributes for different purposes and I don't know if there is an attribute available to do something or even whether to use an attribute to get something done. Is there any documentation of these necessary/helper/nice-to-use attributes?
Look at the classes in System.ComponentModel.DataAnnotations and System.Web.Mvc in Visual Studio's Object Browser.
For some reason the MS documentation is really difficult to find! I came across this post, then ... by chance ... a Jon Galloway video directed me to the following documentation:
System.ComponentModel.DataAnnotations Namespace
This post is 4yrs old, but next time I search I'll find the link here!
For the data annotations, check out this blog post.
For other filters (action filters, authorize filters, etc) I'm not aware of a list anywhere, but according to this article on ASP.net they implement one of four interfaces:
Authorization filters – Implements the IAuthorizationFilter attribute.
Action filters – Implements the IActionFilter attribute.
Result filters – Implements the IResultFilter attribute.
Exception filters – Implements the IExceptionFilter attribute.
The ASP.NET MVC source is available on CodePlex, so you could search it for classes that implement one of those 4 interfaces.

Additional information in ASP.Net MVC View

I am attempting to implement a custom locale service in an MVC 2 webpage. I have an interface IResourceDictionary that provides a couple of methods for accessing resources by culture. This is because I want to avoid the static classes of .Net resources.
The problem is accessing the chosen IResourceDictionary from the views. I have contemplated using the ViewDataDictionary given, creating a base controller from which all my controllers inherits that adds my IResourceDictionary to the ViewData before each action executes.
Then I could call my resource dictionary this way:
(ViewData["Resources"] as IResourceDictionary).GetEntry(params);
Admittedly, this is extremely verbose and ugly, especially in inline code as we are encouraged to use in MVC. Right now I am leaning towards static class access
ResourceDictionary.GetEntry(params);
because it is slightly more elegant. I also thought about adding it to my typed model for each page, which seems more robust than adding it to the ViewData..
What is the preferred way to access my ResourceDictionary from the views? All my views will be using this dictionary.
HtmlHelper extension, which will allow you to call your method like this:
<%: Html.GetEntry(params) %>
Seems a pretty good solution

Resources