Adding Build Configuratior in Asp.Net

Beside “Debug” there is a dropdown, click and select “Configuration Manager”. In “Active Solution Configuration” select “New” then add a name like “Testing” and “Select copy settings from” preferrably copy settings from Release.

Right click “web.config” and select “Add Config Transform”. This will create Web.Testing.config.

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

Showdown

@Html.TextAreaFor(j => j.JobDescription, new { @class = "form-control", @rows = "6", @placeholder = "Enter Job Description" })
        <small class="color-white-mute">Formatting Options: ##heading *italic* **bold** * bulleted list</small>
        <span id="showdown-container">
            <span id="showdown-preview">Preview</span>
        </span>

Scripts

$("#JobDescription").keyup(function () {
    var txtBox = $(this);
    showdownConvert(txtBox);
});
function showdownConvert(txtBox) {
    var converter = new showdown.Converter({ headerLevelStart: 2 });
    $('#showdown-preview').html(converter.makeHtml(txtBox.val()));

    if (txtBox.val()=="") {
        $('#showdown-preview').html("Preview");

    }
}

https://github.com/showdownjs/showdown
http://showdownjs.github.io/demo/
https://mathiasbynens.be/notes/showdown-javascript-jquery

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

Could Not Find Driver in Ubuntu CLI

Note, the PHP version of Apache and Terminal could be different.
Web phpinfo() shows

Loaded Configuration File /etc/php5/apache2/php.ini
PDO drivers | mysql, sqlite

Checking on the terminal

$ php -i |grep php\.ini
Configuration File (php.ini) Path => /etc/php/5.6/cli
Loaded Configuration File => /etc/php/5.6/cli/php.ini

Checking for drivers
$ php -i|grep PDO
PDO
PDO support => enabled
PDO drivers =>

As we see we have no drivers

Ubuntu 14.04.3 LTS
sudo apt-get update
sudo apt-get install php5.6-fpm php5.6-mysql
$ php -i|grep PDO
PDO
PDO support => enabled
PDO drivers => mysql
PDO Driver for MySQL => enabled

Installing PHP 7 with Ubuntu
https://www.unixmen.com/how-to-install-lamp-stack-on-ubuntu-16-04/

To fix “Could Not Find Driver” error
Ubuntu 16.04.1 LTS
sudo apt-get update;
sudo apt-get install php7.0-fpm php7.0-mysql