Can you use environment variables to replace hardcoded path for href attribute in codebase element - clr

I was curious if CLR could load assemblies from a specific location and this is what I found which basically loads the assembly from C:\MyAssemblies folder.
Now, my question is - Is it possible to use environment variables in the following href attribute? In other words, instead of c:\ is it possible to say something like %windir% ?
I am probably going to use GAC for what I need to get done but I was wondering if using environment variables in this context was even possible.
Thanks.
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="MyAssembly2" culture="neutral" publicKeyToken="307041694a995978"/>
<codeBase version="1.0.1524.23149" href="FILE://C:/Myassemblies/MyAssembly2.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Sadly this doesn't seem to be possible, at least not using the standard Microsoft syntax for environment variables of %FOO%.

Related

How to prevent a string property of my model to be converted to US format date in the View?

I discovered something unexpected with my ASP.NET WebApp (Visual 2017 .NET Framework 4.6.1) in a production environment.
I have a model with a property StringDato, which is a STRING following the pattern dd.mm.yyyy hh.mm.ss (the german/european format).
Having faced some issues for parsing, I decided to work with strings for my dates, so there is no a single part of my code where I refer to a DateTime object.
In my development and testing environments (2 different IIS servers), StringDato is displayed as it should be. But in the production environment (a third IIS server) it always shows US formatted.
For example, 23.09.2017 17.45.00 will become 9/23/2017 5:45:00 PM. This happens no matter what the browser is (IE/Edge/Chrome).
The browser is german/european configured, and so is the user's system. The only thing I don't know about yet is how my production server is configured.
I'm trying to get the logic here. Does it make sense to assume the server could "recognize" a string as a date because of its pattern, display it as a date, and format it according to it's config and not the user's settings? What else can I look at to fix this?
It´s simple. Keep Invariant Culture Info on Web.Config:
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<globalization uiCulture="de" culture="de-DE" />
</system.web>

Versioning for binding redirect in app.config .Net

I have binding redirect section in my app.config file.
<dependentAssembly>
<assemblyIdentity name="assemblyName" publicKeyToken="xxxxxxx" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.0.0" />
</dependentAssembly>
My purpose is to redirect any assembly in range of 0.0.0.0 and 2.3.0.0 to version 2.3.0.0. The question is that I don't understand and can't find any articles how binding redirection works for third and fourth block of version (xx.yy.zz.mmmm -> question about zz.mmmm).
Am I right assuming that since I have set zz.mmmm to zero, I'm saying that everything besides major and minor version don't metter and the highest version will be taken and my code covers following situation?
I have two versions. 1.3.8.5918 and 2.3.9.7859. According to my code 1.3.8.5918 will be redirected to 2.3.9.7859?
I have two versions. 2.2.0.1245 and 2.3.8.9865. According to my code 2.2.0.1245 will be redirected to 2.3.8.9865?
The last one. I'm not sure whether it is relevant for me, but since I'm trying to figure out how it works, what if I have
two versions. 2.3.8.5918 and 2.3.9.7859. Will 2.3.8.5918 be redirected to 2.3.9.7859 since for my "binding redirect rule" these two versions are the same i.e. "unspecified"?
Thanks for any information

Programmatically enable or disable anonymous authentication in IIS

I have a web application and I need to provide its users the option to switch login method from FormsAuth to WindowsAuth. I managed to change the web.config file via code:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Url.Content("~"));
AuthenticationSection auth = ((AuthenticationSection)(config.SectionGroups["system.web"].Sections["authentication"]));
auth.Mode = AuthenticationMode.Windows; // Or Forms if I want to.
config.Save();
But the problem is, when I use FormsAuth, I need the Anonymouse Authentication option to be turned on, and when I use WinAuth, I need it to be off. And I just cannot find the way to change that option via code.
Everything on the internet says to do this:
<security>
<authentication>
<anonymousAuthentication enabled="false/true" />
</authentication>
</security>
But when I insert this into my webapp's web.config it says that configuration is wrong. Than I read this might work in another config file, like appHost.config or something like that but I prefer to make changes only to my own application and not to IIS I hope you understand why.
So, how can I do that?
You are trying to update wrong section. anonymousAuthentication is part of system.webServer and not system.web. Correct configuration is
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
You can modify it using ServerManager class found in Microsoft.Web.Administration. Search for nugget package "Microsoft.Web.Administration". Once you have added reference to Microsoft.Web.Administration.dll using nugget you can modify it using code as follows:
using (ServerManager serverManager = new ServerManager())
{
Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration();
Microsoft.Web.Administration.ConfigurationSection anonymousAuthenticationSection = config.GetSection("system.webServer/security/authentication/anonymousAuthentication");
anonymousAuthenticationSection["enabled"] = true;
serverManager.CommitChanges();
}
Okay, so, turned out I could not find a way to dynamically change the auth mode, but I found pretty neat solution to the problem: I have created another web application, nested it inside the first one, had set Forms Auth mode for the first one and Windows for the nested and whenever I needed to use IIS's power to work with domain, user groups etc, I used a redirect from one to another, passing data by cookies and as url parameters.

Is it possible to bind resource file X to publish profile Y?

Let's say I've got an ASP.NET MVC web app with 2 language files (french and german).
For this project, I've got 2 publish profiles (one to xxx.fr and the other to xxx.de).
Is it possible to tell the publish profiles which language file to deploy ?
And if it's possible, how ?
You can create Web.config transforms based on the publish profile. For example, if you have a profile named Foo.pubxml, you can create a transform named Web.Foo.config.
You can set the culture and UI culture in Web.config like this:
<configuration>
<system.web>
<globalization culture="nl" uiCulture="nl"/>
</system.web>
</configuration>
So a transform would look like this:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<globalization culture="fr" uiCulture="fr" xdt:Transform="Replace"/>
</system.web>
</configuration>
By setting the culture, the correct resource files will automatically be used, assuming you follow the naming conventions like Foo.resx as the default (unspecified culture) and Foo.fr.resx for the fr culture.

MVC 5 Migration Issue

I have migrated my project from MVC 4.0 to MVC 5.1. I have followed all the steps as mentioned here
http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2
Afrer that I have installed Vs 2013 SP1.
My project compiles without any issue and runs as expected. But When I open any .cshtml file i'm seeing the following error
The type arguments for method 'System.Web.Mvc.Html.DisplayExtensions.DisplayFor<TModel,TValue>(System.Web.Mvc.HtmlHelper<TModel>,
System.Linq.Expressions.Expression<System.Func<TModel,TValue>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
I tried the following links, no luck.
MVC 5 -> MVC 5.1 Migration. Intellisense issues
mvc 3 to mvc 5 migration razor syntax issue
Any idea?
Ok, atlast I found a solution. But really not sure whether this is what causing the issue. When I looked the web.config file, all the dependentAssembly old version starts with 0.0.0.0. When I created a brand new MVC 5 project, the section starts with 1.0.0.0, when I changed them the above mentioned errors are gone.
But still I'm not convinced with this solution, if anyone find the root cause it'll help us.
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
</dependentAssembly>
All this means is that you're passing ambiguous parameters to DisplayFor. In particular there's an override that accepts a String as a second parameter, in which case, it's a name of a template that should be used to render it, or it could take an Object as a second parameter, in which case, it's extra view data that should be passed to the default template.
If you do something like pass in a "string" from ViewBag, like:
#Html.DisplayFor(m => m.Something, ViewBag.UseThisTemplate)
You're actually passing an object because ViewBag is not typed. This is just one example, but the long and short is that you're just not being explicit enough about what parameters you're passing and what their types are.

Resources