Web.config equivalent to “using static MyClass;” in <add namespace=“MyClass” /> - razor

In ASP.Net, you can add a namespace to all Razor views by adding the following code to the View folder’s Web.config:
<system.web.webPages.razor>
<namespaces>
<add namespace=“MyClass” />
</namespaces>
</system.web.webPages.razor>
This is equivalent to putting the statement “using MyClass;” at the top of a C# file.
However, how would I add a namespace to Web.config as a “static” class, where I can access the class’s methods directly within views without having to write out “MyClass.MyMethod();” for example?
You can already do this by putting the statement “using static MyClass;” at the top of a C# file (C# 6 is required, see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static).

It just appends whatever it is you put as namespace. Just needs to append the static keyword to the namespace.
So as you pointed out <add namespace=“MyClass” /> is equivalent to using MyClass;
Change to <add namespace=“static MyClass” />, which is equivalent to using static MyClass;
In your case:
<system.web.webPages.razor>
<namespaces>
<add namespace=“static MyClass” />
</namespaces>
</system.web.webPages.razor>
should be what you're looking for

Related

How to use two connection strings on development vs production

I have two connection strings in my Web.config.
<connectionStrings>
<add name="AuthContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=mokey;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="AuthContextMySQL" providerName="MySql.Data.MySqlClient" connectionString="database=mokey;persistsecurityinfo=True" />
</connectionStrings>
I can specify which one to use in code like this:
public class AuthContext : IdentityDbContext<IdentityUser>
{
public AuthContext()
: base("AuthContextMySQL")
{
}
}
How do I specify which one to use depending on development vs production?
You can use web.config transformations during your build step to add/update/delete sections of your web.config depending on the build configuration. See this article for a step by step guide.

how to add configuration for log4net (or any other 3rd party library) in ASP.NET 5.0

I was reading Scott Gu's blog on ASP.NET 5.0 features and one of the new feature mentioned in the blog is to use json file as configuration and elimination of Web.config file.
I have few questions around that feature.
Assume I have following log4net configuration which was previously added to Web.Config in previous version of ASP.NET
Config file
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\TestProj\\TestLog.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
How would one add sections in config.json ?
How would one convert above xml and add it to config.json?
Does 3rd Party library ( In my example log4net ) or users of the library have to add some type of custom conversion api to support json based configuration, in order to take advantage of new configuration feature provided in ASP.NET 5.0?
Recently I had the same problem with log4net. I managed to do it working as following:
Create log4net.xml file containing the configuration section for log4net
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
...
</log4net>
You can put the file in the project root folder.
And in the Startup.Startup() method you can configure the log4net providing the xml as a configuration:
public Startup(IApplicationEnvironment appEnv)
{
// ...
XmlConfigurator.Configure(new FileInfo(Path.Combine(appEnv.ApplicationBasePath, "log4net.xml")));
}
I hope this helps.
Current versions of log4net don't support Json projects, so the config needs to be in a separate xml file.

How use MySQL membership in mvc4 code first application?

I am new in MVC with MySQL. I have code first application in MVC 4 with MySQL.
public class Category
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public virtual IEnumerable<Product> Products { get; set; }
}
public class Product
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public DateTime CreateDate { get; set; }
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
}
And in web.config:
<connectionStrings>
<add name="MyContext" connectionString="server=localhost; User Id=root; Pwd=mypass; Persist Security Info=True; database=catalogue" providerName="MySql.Data.MySqlClient " />
</connectionStrings>
So, this creates catalogue database in mysql. But i want also to use membership of mysql. To register, to create users and manage their roles.
For example, using default AccountModels of mvc4
I added this code to web.config:
<system.web>
<membership defaultProvider="MySQLMembershipProvider">
<providers>
<clear />
<remove name="MySQLMembershipProvider" />
<add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" applicationName="/" description="MySQL default application" connectionStringName="LocalMySqlServer" writeExceptionsToEventLog="True" autogenerateschema="True" enablePasswordRetrieval="True" enablePasswordReset="True" requiresQuestionAndAnswer="False" requiresUniqueEmail="False" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
</providers>
</membership>
<profile defaultProvider="MySQLProfileProvider">
<providers>
<clear />
<remove name="MySQLProfileProvider" />
<add name="MySQLProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" applicationName="/" description="Profiles" connectionStringName="LocalMySqlServer" writeExceptionsToEventLog="False" autogenerateschema="True" />
</providers>
</profile>
<roleManager enabled="false" defaultProvider="MySQLRoleProvider">
<providers>
<clear />
<remove name="MySQLRoleProvider" />
<add name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" applicationName="/" description="Project Roles" connectionStringName="LocalMySqlServer" writeExceptionsToEventLog="True" autogenerateschema="True" />
</providers>
</roleManager>
</system.web>
When run application I get this error:
Which step have I forgot? I cant use membership.
(Sorry for bad English)
If what you want is to use the Membership Provider that comes shipped with MVC4 Internet Templates of VS2010 or VS2012, with a database other than SQL Server Family, namely SQL Server, SQL Compact and Azure, I think that is not feasible, since MVC4 uses SimpleMembership provider, which is not Database Agnostic.
Have said that, that doesn't mean that you can't achieve what you want. I propose to use Microsoft ASP.NET Universal Providers Core Libraries. I recommend that you read this excellent Post
Now in this article the author, Scott Hanselman, mentions the following
Using these Universal "Default Profile Providers" means all you have to do is set the right connection string and your applications that use these services will work with SQL Server (plus Express), SQL Server Compact and SQL Azure with no code changes from you.
Hmmm and how about other SQL Standards?, well let's follow these steps,
1) Make sure to change the connection string first, and then with the Nuget Package Manager, install MySQL.Web, and MySQL.Data.
<connectionStrings>
<add name="MySQLConn" connectionString="Server=localhost;Database=dbname;Uid=dbuser;Pwd=dbpass;" />
</connectionStrings>
2) With the Nuget Package Manager find and install the "Microsoft ASP.NET Universal Providers Core Libraries" or run the following commands in the Package Manager Console,
Install-Package system.web.providers
Install-Package Microsoft.AspNet.Providers.Core
3) Go to Project Menu -> ASP.Net Configuration, you will see in the browser ASP Net Administration tool for your web project.
4) Then go to Provider Tab, Go to "Select a different provider for each feature (advanced)"
5) Make sure to select MySQL Membership Provider.
6) Then go Security Tab, and create some Roles and Users that you consider necessary for your project.
If for some reason you get an error here, try changing your web config, and replace Membership, Role and Profile with this
7) Enjoy!
I hope this helps
PS: I'm very new on this too, so I'm open to suggestion about the best Membership Provider for MVC4 that targets others SQL Databases.
As a side note, you can not use the same Account Controller, of the Internet Template of ASP Net MVC3 or MVC4 of VS2010 or VS2012. Though you have to implement that all by yourself.
You can use for example Security Guard if you want to quickly add MVC template for Login/Logoff/Role Administration/Create Users etc.
The method that I proposed is not a patch nor is hackerish, ASP Net Membership Provider were long implemented and promoted by Microsoft and MySQL added the functionality to the Net. Connector, and they even have posted about it. Here
So fear not, you can safely use this method, unless there is a strong argument against using ASP Net Membership Provider, that I'm not aware of.
This is taken from GridWizzard - you just need set enableSimpleMembership to false under appsettings, http://gridwizard.wordpress.com/2014/03/05/asp-net-mvc4-just-get-it-running-without-having-to-implement-membershiproleprovider/

BUG: IIS7 managed requests

(I don't know whether should I also post this question to ServerFault, since it's about IIS configuration?)
In IIS7 we can tell a module to run for managed content (thus speeding up static content serving) by:
<modules>
...
<add name="WhateverName"
type="WhateverType"
preCondition="managedHandler"
...
</modules>
But. This works fine and dandy as long as there's also a file name (with extension) in the requested URL. If it's omitted it IIS7 will think you want static content and managed modules won't run.
http://localhost/ <-- this one will skip managed handlers
http://localhost/default.aspx <-- this one will run them
If I manually set IIS7 default document, so the first one is default.aspx, I can see no difference there's no difference. To me this looks, walks and sounds like a bug. And it is a bug! Why? Because when I request for the first one, it is a managed request, isn't it. Of course it is. But IIS7 treats it as a static request. So? It's a bug. This request should be treated as managed.
How can I convince IIS7 to run managed handlers for URL requests without file names inside?
Help with thinking
Let me help you a bit with thinking: If I'd reorder system.webServer/handlers, I'm sure could solve this. Before the last StaticFile handler that points to StaticFileModule, DefaultDocumentModule and DirectoryBrowsingModule I should be running integrated asp.net handler on Directory requests. Or write my own handler, that would append default document to any directory request. I'm pretty sure one of these should solve it. But how would I have to configure/develop it?
The problem is in request-processing order. IIS7 processes requests in order specified by Handlers configuration element of IIS. By default Handlers element of the IIS configuration contains
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
at the end of the handlers. Therefore all request that not match any previously specified handler, will be processed by this handler (including folder request too).
You can remove all default handlers by using clear element in handlers configuration and specify your own request processing order.
I recommend to copy default IIS handlers configuration (C:\Windows\System32\inetsrv\config\applicationHost.config) to your web config without StaticFile handler at the end.
Then you should add specific static content handler for each static content type (jpg, gif, js, css).
<add name="StaticFile-swf" path="*.swf" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFile-png" path="*.png" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFile-gif" path="*.gif" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFile-jpg" path="*.jpg" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFile-css" path="*.css" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="StaticFile-js" path="*.js" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
and manged handler (PageHandlerFactory) for folder requests after that.
<add name="PageHandlerFactory-Folders" path="*" verb="*" type="System.Web.UI.PageHandlerFactory" modules="ManagedPipelineHandler" resourceType="Unspecified" requireAccess="Read" allowPathInfo="false" preCondition="integratedMode" />
At the end you should also add StaticFile handler.
Here is an example.
Removing preCondition="managedHandler" or adding <modules runAllManagedModulesForAllRequests="true"> should do it. The "Preconditions" section of this page has more information.
You can use a wild card script mapping, but it's inefficient to use the managed handler to handle all requests. The static handler is much more efficient when it is appropriate.

How to send email in HTML format with Microsoft Enterprise Library?

I know how to send mails using the Microsoft Enterprise Library 2.0 using a text formatter. But these emails are always in plain text. Is there any way with entlib 2.0 to send these mails in HTML format?
Well that is funny, I am now writing my own answer.
What I did was use the source code of entlib.
Within
Microsoft.Practices.EnterpriseLibrary.Logging and
Microsoft.Practices.EnterpriseLibrary.Logging.TraceListenerData
I found the classes that I needed.
Copy EmailMessage.cs to EmailMessageHTML.cs
Copy EmailTraceListener.cs to EmailHTMLTraceListener.cs
Copy EmailTraceListenerData.cs to EmailHTMLTraceListenerData.cs
Put these classes in your own new Library Project.
Within EmailMessageHTML change all constructors to match the new classname and than ADD following line to the method:
protected MailMessage CreateMailMessage()
{
.....
message.IsBodyHtml = true;
.....
return message;
}
After that, I had to use this new EmailMessageHTML class in EmailHTMLTraceListener (change EmailMessage to EmailMessageHTML) and also use this EmailHTMLTraceListener in the new EmailHTMLTraceListenerData.cs file.
Compile this new project and than use this in your config as follows (example)
<loggingConfiguration
name="Logging Application Block"
tracingEnabled="true"
defaultCategory=""
logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add toAddress="your#emailgoes.here"
fromAddress="yourserveraddress#goes.here"
subjectLineStarter=""
subjectLineEnder="My HTMLemailLogger"
smtpServer="localhost" smtpPort="25"
formatter="Text Formatter"
listenerDataType="MYLibrary.HTMLEmailLogger.EmailHTMLTraceListenerData,
MYLibrary.HTMLEmailLogger, Version=2.0.0.0,
Culture=neutral,
PublicKeyToken=null"
traceOutputOptions="None"
type="MYLibrary.HTMLEmailLogger.EmailHTMLTraceListener,
MYLibrary.HTMLEmailLogger,
Version=2.0.0.0,
Culture=neutral,
PublicKeyToken=null"
name="EmailHTML TraceListener"/>
</listeners>
</loggingConfiguration>
and add a valid category to log this to of course:
<add switchValue="All" name="OutOfBalanceBooking">
<listeners>
<add name="Database Trace Listener"/>
<add name="EmailHTML TraceListener"/>
</listeners>
</add>
Of course you need some HTML document to than log with EntLib. I leave that as an exercise for the reader.
And indeed! I get a nice HTML email now for every outofbalance booking that customers make on the site...