一段時間後,我設法得到大它的一部分工作(html5mode除外)。因此,如果我們堅持使用散列標籤,它的所有工作......讓我們看到代碼
角應用聲明:
var app = angular.module('main', ['ngCookies', 'ngRoute', 'ngGrid', 'ngResource', 'ui.bootstrap']); //Define the main module
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
//Setup routes to load partial templates from server. TemplateUrl is the location for the server view (Razor .cshtml view)
$routeProvider
.when('/home', { templateUrl: '/home/main', controller: 'MainController' })
.when('/contact', { templateUrl: '/home/contact', controller: 'ContactController' })
.when('/about', { templateUrl: '/home/about', controller: 'AboutController' })
.when('/demo', { templateUrl: '/home/demo', controller: 'DemoController' })
.when('/angular', { templateUrl: '/home/angular' })
.otherwise({ redirectTo: '/home' });
}]);
的Shared\_Layout.cshtml
代碼
<head>
<base id="basePath" href="/@((ViewContext.RouteData.Values["language"] != null) ? ViewContext.RouteData.Values["language"] : "en")/" />
</head>
<body>
<!-- the rest of your bootstrap navbar code here... -->
<li class="dropdown">
<a href='' class="dropdown-toggle" data-toggle="dropdown">Pages<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href='#/home'>Home</a></li>
<li><a href="#/demo">Demo</a></li>
<li><a href='#/angular'>Learn Angular</a></li>
<li><a href='#/about'>About</a></li>
<li><a href='#/contact'>Contact</a></li>
</ul>
</li>
</body>
的App_Start\RouteConfig.cs
代碼
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{language}/{controller}/{action}/{id}",
defaults: new
{
language = "en",
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
culture = "CA"
},
constraints: new { language = @"^[a-zA-Z]{2}$" }
);
routes.MapRoute(
name: "DefaultWithoutLang",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
}
}
後來終於裏面Global.asax
文件,我添加了這個新功能
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
string connectedUsername = "";
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
connectedUsername = HttpContext.Current.User.Identity.Name;
}
var handler = Context.Handler as MvcHandler;
var routeData = handler != null ? handler.RequestContext.RouteData : null;
var routeCulture = (routeData != null && routeData.Values.ContainsKey("language")) ? routeData.Values["language"].ToString() : null;
var phpsessionid = HttpContext.Current.Request.Cookies["PHPSESSID"];
var languageCookie = HttpContext.Current.Request.Cookies["language"];
var userLanguages = HttpContext.Current.Request.UserLanguages;
// Set the Culture based on a route, a cookie or the browser settings,
// or default value if something went wrong
var cultureInfo = new CultureInfo(
routeCulture ?? (
languageCookie != null
? languageCookie.Value
: userLanguages != null
? userLanguages[0]
: "en")
);
// save language in a cookie (fr, en, es, ...)
var langCookie = new HttpCookie("language", cultureInfo.TwoLetterISOLanguageName);
HttpContext.Current.Response.AppendCookie(langCookie);
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
}
所以它都工作,除了html5mode,如前所述。如果有人也可以幫我解決這個問題,我會很高興......我在某處讀到我可能需要一箇中間件來捕捉404並以某種方式進行轉換,但我不太明白這一部分。
隨意如果任何錯誤都發現糾正我的代碼(仍然是新的ASP和角度)。