HtmlString vs. MvcHtmlString - .net-4.0

HtmlString vs. MvcHtmlString
What are the differences bettween those two, or when to prefer one over the other?
Edit:
One thing to prefer MvcHtmlString over HtmlString is the extension method IsNullOrEmpty of MvcHtmlString.

HtmlString only exists in ASP.NET 4.
MvcHtmlString was a compatibility shim added to MVC 2 to support both .NET 3.5 and .NET 4. Now that MVC 3 is .NET 4 only, it's a fairly trivial subclass of HtmlString presumably for MVC 2->3 for source compatibility.
If you're ever going to drop back to MVC 2 it might make sense to use IHtmlString or var for values returned from MVC functions. Alternatively I think you can now just switch to HtmlString throughout.

HtmlString was only introduced in .Net 4.0.
In ASP.Net 3.5 MVC 2.0, you should use MvcHtmlString.
In .Net 4.0, you should use HtmlString. (which is simpler)

MvcHtmlString is used to indicate when a string should not be re-encoded.
See What is an MvcHtmlString and when should I use it?
and
What does <%: %> do in ASP.NET (MVC2)?

Related

How can the below line be replaced in .net4.0

How can I write below line of code in .net 4.0:
EventHandlerTaskAsyncHelper asyncHelper = new EventHandlerTaskAsyncHelper(WriteLogMessages);--> this is in .net 4.5
Thanks
EventHandlerTaskAsyncHelper is simply a helper that provides an APM-style (BeginOperation/EndOperation) interface over a Task. A Task does implement IAsyncResult so you can return it directly from a BeginOperation method. The EndOperation method only has to cast its IAsyncResult argument back to a Task and await it. This is shown in the MSDN article
TPL and Traditional .NET Framework Asynchronous Programming:
public IAsyncResult BeginCalculate(int decimalPlaces, AsyncCallback ac, object state)
{
Task<string> f = Task<string>.Factory.StartNew(_ => Compute(decimalPlaces), state);
if (ac != null) f.ContinueWith((res) => ac(f));
return f;
}
public string Compute(int numPlaces)
{
...
}
public string EndCalculate(IAsyncResult ar)
{
return ((Task<string>)ar).Result;
}
The EventHandlerTaskAsyncHelper class just makes it easier to write such code, with some checks for already completed tasks etc. The concept isn't something specific to .NET 4.5.
That said, the best solution would be to upgrade the rest of your code to .NET 4.5, not try to backport the application to .NET 4.0. While you can get some of the 4.5 functionality with the Microsoft.Bcl.Async package, significant parts will be missing. Newer libraries like TPL Dataflow, Immutable Collections etc simply require .NET 4.5 to work.
Unless you target Windows XP there is no reason to remain in .NET 4.0, especially when the application is already written in 4.5.
The main problem of such migration is that you are trying to fit into architecture solution available only in .NET 4.5. According MSDN, EventHandlerTaskAsyncHelper the only purpose is:
Converts task-returning asynchronous methods into methods that use the asynchronous programming model used in previous versions of ASP.NET and that is based on begin and end events.
And Remarks section:
To handle asynchronous tasks in ASP.NET 4.5, you implement the logic to return a task as a TaskEventHandler delegate. This model of asynchronous task-based programming supersedes the model used in previous versions of ASP.NET, which bases all event handling on begin and end events. ...
So this class is used for the wrapping other code to fit into the new version of ASSP.NET MVC site. Thus you have to reconsider the whole class diagram and architecture of your application according rules of the MVC version you'll be using in your project.
May be this thread will be a good start:
ASP.NET MVC - IHttpModule, Asynchronous Event Handler, EventHandlerTaskAsyncHelper

Where is source code for default model binder for decimal in asp.net mvc 4

I'm looking for source code for default model binder for decimal in asp.net mvc 4.
I searched assemblies with dotpeek, but I cannot find it.
Where it is placed?
I believe decimal instances are bound using the DefaultModelBinder class, which source you can find here: http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/DefaultModelBinder.cs

C# WCF REST - How do you use JSON.Net serializer instead of the default DataContractSerializer?

In .NET 3.5, Is it possible to override the default DataContractJsonSerializer and use the JSON.net serializer instead?
NOTE: We do not want to use attributes on the class
Yes, it's possible to do so. But it's not too simple. You'll need a new message formatter which uses the JSON.NET serializer instead of the default one to convert between the operation parameters and the message object needed by the WCF stack. The post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx has an example that does exactly that. The code is written for 4.0, but it should work for 3.5 as well.

System.ComponentModel.DataAnnotations.compare vs System.Web.Mvc.Compare

MVC 4 Beta project fails to compile after upgrading to .Net 4.5.
This happens due to conflict between
System.ComponentModel.DataAnnotations.CompareAttribute and System.Web.Mvc.CompareAttribute
System.ComponentModel.DataAnnotations.CompareAttribute MSDN documentation says:
Provides an attribute that compares two properties.
While System.Web.Mvc.CompareAttribute MSDN documentation says:
Provides an attribute that compares two properties of a model.
What is the difference between the two and when it would be "smarter" to use each of them?
10x.
So, looking at the MSDN documentation and doing a literal comparison of the two classes, I noticed both classes are derived from System.ComponentModel.DataAnnotations.ValidationAttribute. In fact, the classes are almost exactly the same. The only notable difference is that the MVC version also implements IClientValidatable which adds the following properties:
FormatPropertyForClientValidation - (static member) Formats the property for client validation by prepending an asterisk and a dot.
GetClientValidationRules - Gets a list of compare-value client validation rules for the property using the specified model metadata and controller context.
As for which class you should use, if the model will be directly bound to a view, use the MVC version so that you can take advantage of the client-side validation. However, if you're using ViewModels, you can stick with the ComponentModel class and avoid the unnecessary overhead of the additional properties. Your call!
System.Web.Mvc.CompareAttribute
System.ComponentModel.DataAnnotations.CompareAttribute
Microsoft Connect work-around is:
Posted by GavK on 6/17/2012 at 5:13 AM
I added a full reference to [System.Web.Mvc.Compare(...)] rather than
just using [Compare(...)]
Works for me in VS 2012...
Vinney nailed most of it with the exception of which one you should use...
The reason you have a conflict after changing your target framework to 4.5 is because prior to .NET 4.5 there was no CompareAttribute class in the System.ComponentModel.DataAnnotations namespace and the class defined under System.Web.Mvc filled the gap. Therefore, as an example if you were using [Compare] and [Required] attributes in your model class prior to updating your target framework you ended up with a conflict when you upgraded.
Assuming you are not using anything else in the System.Web.Mvc namespace in your model class, you should remove that using statement and let it rely on the System.ComponentModel.DataAnnotations namespace. Unobtrusive client-side validation will continue to work exactly as it did before, just as it does for other attributes that you decorate your model's properties with from the same namespace (eg Required).
If you wish to be explicit about the reference, you can simply add this line:
using CompareAttribute = System.Web.Mvc.CompareAttribute;
Using Visual Studio 2013 (MVC 5 project, .NET 4.5) the IntelliSense suggests that System.Web.Mvc.CompareAttribute is deprecated.
I used System.ComponentModel.DataAnnotations.CompareAttribute and it works fine.
It also does the client-side validation!
On this post, they also suggest another solution, which is to move the reference of the preferred namespace for Compare() inside the model's namespace. Eg. if you prefer to use Compare from System.Web.Mvc, use:
using System.ComponentModel.DataAnnotations;
namespace MyProject.MyViewModel
{
using System.Web.Mvc;
The compiler will search inside the model's namespace first.

Created a class that inherits from NinjectModule, where to load now?

After creating a class that inherits from NinjectModule, and overriding the Load() method with all my binding calls, where do I setup ninject in my asp.net web application? (MVC)
Is it a httpmodule that I have to create? global.asax?
UPDATE NB see comment on question - this only makes sense for MVC if using a very old Ninject (2 or earlier) and a very old MVC (2 or earlier)
There's a NinjectHttpApplication you derive from which auto-injects pages.
See Does anyone know of a good guide to get Ninject 2 working in ASP.NET MVC?