Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Better handling of empty view path:
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards committed Dec 18, 2015
1 parent 87cd09b commit 01fdb80
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Microsoft.AspNet.Mvc.Localization/ViewLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using Microsoft.AspNet.Mvc.Rendering;
Expand Down Expand Up @@ -95,16 +96,27 @@ public void Contextualize(ViewContext viewContext)
throw new ArgumentNullException(nameof(viewContext));
}

// Given a view path "/Views/Home/Index.cshtml" we want a baseName like "MyApplication.Views.Home.Index"

// Trim the file extension from the end of the path
var path = viewContext.ExecutingFilePath;
if (Path.HasExtension(path))
if (!string.IsNullOrEmpty(path) && Path.HasExtension(path))
{
var extension = Path.GetExtension(path);
path = path.Substring(0, path.Length - extension.Length);
}

if (string.IsNullOrEmpty(path))
{
path = viewContext.View.Path;
}

Debug.Assert(!string.IsNullOrEmpty(path), "Couldn't determine a path for the view");

var baseName = path.Replace('/', '.').Replace('\\', '.');
baseName = baseName.TrimStart('.');

// Prepend the application name
baseName = _applicationName + "." + baseName;

_localizer = _localizerFactory.Create(baseName, _applicationName);
Expand Down

0 comments on commit 01fdb80

Please sign in to comment.