MVC3 VS2010 Application Settings i9mplementation - asp.net-mvc-3

I have an MVC2 C#.Net web app using VS2010. Below is an entry in y web.config:
<configuration>
<applicationSettings>
<BOE.My.MySettings>
<setting name="AppBackColor" serializeAs="String">
<value>AntiqueWhite</value>
</setting>
</BOE.My.MySettings>
</applicationSettings>
</configuration>
However, in my Controler.cs file My.Settings.AppBackColor is unrecognized. Any ideas?
var backColor= My.Settings.AppBackColor
What am I doing wrong here?

That's Desktop application stuff. In web applications you don't use such settings.
You could use the <appSettings> section of your web.config to store custom values:
<appSettings>
<add key="foo" value="bar" />
</appSettings>
and then when you want to read foo:
var foo = ConfigurationManager.AppSettings["foo"];

Related

How do I set the environment in a Visual Studio generated publish profile

I have 2 publish profiles. I want to make one staging and one production. After browsing a bit I found out you could use the command line publish to set an environment variable: dotnet publish /p:Configuration=Release /p:EnvironmentName=Staging. However my publish profiles are generated by Visual Studio and I was wondering if I could set it in there?
Although I didn't tried this approach on .Net Core, I believe that It will work since It is the feature of VS Publish (msbuild).
you need to create web.config. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<remove name="WebDAV" />
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<modules>
<remove name="WebDAVModule" />
</modules>
<aspNetCore requestTimeout="00:30:00" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
then you need to add Config Transform for these (see below)
For the config transformation you can check this github documentation: https://github.com/vijayrkn/webconfigtransform/blob/master/README.md
then you need to transform your publish profile as well.
web.$(publish-profile-name).config is going to be your web.config name. You need to change environment variables of the transformed web.configs respectively.

Where is the package sources location

I use VS 2015 .Where in the visual studio project store the setting of Tools>options >Nuget Package Manager >Package Sources's setting. Currently Nuget.org is checked for Package Sources.I want to point this to a different location other than https://www.nuget.org/api/v2/, by editing the file location.
I checked the project file csproj, but did not find it
I need to change this manually in the server to point to the server repository. Working fine in local machine but not in the server.
This is a per-user setting, and is stored in %APPDATA%\NuGet\NuGet.Config. The file looks like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="Abc" value="http://def/nuget/" />
<add key="Package source" value="http://packagesource" />
</packageSources>
<packageRestore>
<add key="enabled" value="True" />
<add key="automatic" value="True" />
</packageRestore>
<bindingRedirects>
<add key="skip" value="False" />
</bindingRedirects>
</configuration>
Simply add another entry into the <packageSources> element. (Mine is obviously updated for the protocol version 3 and so is referencing https://api.nuget.org/v3/index.json rather than https://www.nuget.org/api/v2/).

web.config transform: not the debug.cofig values when debugging locally

I am trying to change the values of some appsetting keys while running locally.
So, i have my original Web.config which has thoses keys
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="config" value="Release" />
</appSettings>
I am trying to change the value of "config" key in the web.debug.config,
<?xml version="1.0"?>
<!-- For more information on using Web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=301874 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="config" value="Debug" xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>
<system.web>
</system.web>
</configuration>
When, i am running the project locally using VS:
I am printing the value of config and is always showing the value from web.config, however when i click preview transformation on web.debug.config, its showing this:
how can i make it work locally by picking information from web.debug.config?

How do you add a mime type when using ASP.NET vNext?

There's LOADS of information on how to add MIME types into a normal project.
These include configuring IIS, or modifying the web.config.
Both of these options are unavailable to me in vNext with IIS Express.
I had a look at the schema to the project.json file and couldn't find anything in there that would help either.
Can this be done yet? - I want to add a mime type for the .woff2 extension.
If you hosting it on IIS 7 or later then following step will do what you need. This answer I have used Visual Studio 2015 CTP5.
Publish your web application ( ASP.net vnext)
You can publish it to location like C:\MyPublish
Once it get successfully published you will find following location C:\MyPublish\wwwroot. Here You will find web.config.
Now host your site to in IIS ( Make sure that you have used C:\MyPublish\wwwroot as your path)
Now edit web.config over here just like you did for old version to add mime type. ( Following is my edit)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="kpm-package-path" value="..\approot\packages" />
<add key="bootstrapper-version" value="1.0.0-beta2" />
<add key="kre-package-path" value="..\approot\packages" />
<add key="kre-version" value="1.0.0-beta2" />
<add key="kre-clr" value="CLR" />
<add key="kre-app-base" value="..\approot\src\WebApplication5" />
</appSettings>
<system.webServer>
<staticContent>
<remove fileExtension=".woff" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
</staticContent>
</system.webServer>
</configuration>
Note: As per my thinking In old version it is fix that it is always windows environment so we have direct web.config file in project and we edit that but now we have to follow different process to register as in future we can host completly on linux env as well.
Update : There is another way to do that as well. If you are using Microsoft.AspNet.StaticFiles package then you will have extension.
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
}
This will indirectly use https://github.com/aspnet/StaticFiles/blob/dev/src/Microsoft.AspNet.StaticFiles/FileExtensionContentTypeProvider.cs. Here you can see all mapping.
Update 2: (Add New Mime Type)
public void Configure(IApplicationBuilder app)
{
StaticFileOptions option = new StaticFileOptions();
FileExtensionContentTypeProvider contentTypeProvider = (FileExtensionContentTypeProvider)option.ContentTypeProvider;
contentTypeProvider.Mappings.Add("<<yourextention>>","<<mimetype>>");
app.UseStaticFiles(option);
}
Until this is released, you can also edit applicationhost.config which I found in D:\Documents\IISExpress\config (yours might be on your C drive [Documents]).
I added:
<mimeMap fileExtension=".woff2" mimeType="font/x-woff2" />
Inside <staticContent>.

WebMatrix WebSecurity: Keeps logging out user randomly

I'm using WebMatrix and applied a login system on my website. I'm running into a strange problem. My website keeps randomly logging out the user. Happens unexpectedly. Not only on my code, but happens in the WebMatrix sample projects as well.
My Web.Config file is:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="loginUrl" value="~/login" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<sessionState timeout="20" />
<!-- This is for links with incorrect file extensions -->
<!-- This only handles .NET based errors, not classic web like HTML or ASP based extensions -->
<customErrors mode="Off">
<error statusCode="403" redirect="/Shared/Error404.cshtml" />
<error statusCode="404" redirect="/Shared/Error404.cshtml" />
<error statusCode="500" redirect="/Shared/Error500.cshtml" />
</customErrors>
</system.web>
<system.webServer>
<!-- This handles all other types of link errors -->
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/Shared/Error404.cshtml" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add invariant="System.Data.SqlServerCe.4.0" name="Microsoft® SQL Server® Compact 4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
</configuration>
I've defined the session timeout as 20 minutes. I use the same plain way of logging in the user.
if (WebSecurity.Login(email, password, rememberMe)) {
Context.RedirectLocal(returnUrl);
return;
} else {
ModelState.AddFormError("The user name or password provided is incorrect.");
}
And this is how I check whether my user is signed-in or not in secured pages:
if (!WebSecurity.IsAuthenticated) {
Response.Redirect("~/login", true);
}
I've searched a bit on google, there are few people who have also been complaining that WebMatrix WebSecurity randomly logs out user. Sometimes when an activity is performed, like form submission or sometimes a simple url click.
Any ideas or suggestions? Someone even suggested me to dump Razor and move to MVC, it doesn't have this problem. I'm not sure whether thats true.
UPDATE
I have the following code in top of all my secured pages (which require user being logged in). Does anyone think this would be causing the issue?
// Ensure this page is not cached
Response.Expires = -1;
Response.Cache.SetNoServerCaching();
Response.Cache.SetAllowResponseInBrowserHistory(false);
Response.CacheControl = "no-cache";
Response.Cache.SetNoStore();
Thanks
-Faraz Azhar
I will have to check further, i remember some weirdo setting to increase the cookie stay-logged-in time. Here's what's in my config and i don't see anything else that has a time:
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/error/"></customErrors>
<compilation debug="true" targetFramework="4.0" />
<authentication>
<forms timeout="10080" />
</authentication>

Resources