Securing Configuration Settings


<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" />
</appSettings>

Create another config file like AppSettings.config and in web.config > appSettings, add configSource attribute and specify the file

<appSettings configSource="AppSettings.config"></appSettings>

This is useful in public repositories and other scenarious where you don’t want to share your other credentials but to specific people only.
Exclude the AppSettings.config from repository

Increasing IIS Upload Limit

When uploading file to IIS greater than default will lead to
Exception Details: System.Web.HttpException: Maximum request length exceeded.

To solve this problem, we need specify our upload limit the server is allowed to accept say 5MB (1024×5000)
In Web.config > system.web add
<httpRuntime maxRequestLength="5120000" executionTimeout="3600" />

In Web.config > system.webServer
<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="5120000" />
    </requestFiltering>
</security>

Note: maxRequestLength and maxAllowedContentLength must match

Customizing PagedList.Mvc

@Html.PagedListPager(
Model.Jobs,
page => Url.Action("Jobs",
new { controller = "Company", area = "Admin", page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }),
new PagedListRenderOptions
{
LinkToFirstPageFormat = "<<",
LinkToLastPageFormat = ">>",
LinkToNextPageFormat = "",
LinkToPreviousPageFormat = "",
UlElementClasses = new[] { "pagination","pagination-theme" }
}
)

More info about PagedList.Mvc https://github.com/TroyGoode/PagedList/blob/master/src/PagedList.Mvc/PagedListRenderOptions.cs

Entity Framework 6 Transaction

using (var dbContextTransaction = dbContext.Database.BeginTransaction())
{
try
{
var job = new Job{Name = "Name"}
dbContext.Jobs.Add(job);
dbContext.SaveChanges();

var companyJobs = new CompanyJob{Name="Name",JobId = Job.Id};
dbContext.CompanyJobs.Add(companyJobs);
dbContext.SaveChanges();
dbContextTransaction.Commit();
}
catch (Exception)
{
dbContextTransaction.Rollback();
}
}

Another method is using scope, pass TransactionScopeAsyncFlowOption.Enabled to TransactionScope() constructor to allow async saving method

using (var transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
var job = new Job{Name = "Name"}
dbContext.Jobs.Add(job);
dbContext.SaveChangesAsync();

var category = dbContext.Categories.Find(2);
job.Categories.Add(category);
dbContext.SaveChangesAsync();

transactionScope.Complete();
}

More ways here
https://msdn.microsoft.com/en-us/library/dn456843(v=vs.113).aspx

Dealing with One-to-Many Relationship in Fluent API

For example, a Company has many Jobs, in Company Configuration add
HasMany(c => c.Jobs)
.WithRequired(j => j.Company)
.HasForeignKey(j => j.CompanyId);

Without the above configuration, Company navigation property of Job will not work.

While in Job Configuration
HasRequired(j => j.Company)
.WithMany(c => c.Jobs)
.HasForeignKey(j => j.CompanyId);

Add the 2 configuration so that their navigational properties will work.

Adding Glimpse to ASP.Net

Glipse is a tool to monitor the performance of web app.

install-package glimpse.mvc5
Install-Package Glimpse.EF5

If you encounter this problem
Unable to cast object of type 'Glimpse.Ado.AlternateType.GlimpseDbConnection' to type 'System.Data.SqlClient.SqlConnection'.
Uninstall Glimpse.EF5 and install
Install-Package Glimpse.EF6
You can view Glimpse endpoint in /glimpse.axd

Asp.net Web API 2

1. Create new folder.
2. Right click on the newly created folder, select Add > Controller > Web API 2 Controller – Empty

A readme.txt will open containing the instruction.

Visual Studio has added the full set of dependencies for ASP.NET Web API 2 to project .

The Global.asax.cs file in the project may require additional changes to enable ASP.NET Web API.

1. Add the following namespace references:

using System.Web.Http;
using System.Web.Routing;

2. If the code does not already define an Application_Start method, add the following method:

protected void Application_Start()
{
}

3. Add the following lines to the beginning of the Application_Start method:

GlobalConfiguration.Configure(WebApiConfig.Register);