1

我開始與ASP MVC和AngularJS項目的干擾,我想有ASP MVC從網址爲這樣的事中處理的定位:http://localhost:4416/en/#/contact或這http://localhost:4416/en-US/#/home,所以本地化是在哈希標籤之前設置的,我希望ASP MVC Route來處理它,因爲我也使用Razor查看頁面。在ASP MVC的前一個項目而已,我有這種類型的途徑來處理語言和文化:我怎樣才能讓ASP MVC本土化路線不與AngularJS路線

routes.MapRoute(
     name: "Default", 
     url: "{language}/{controller}/{action}/{id}", 
     defaults: new 
     { 
     controller = "Home", 
     action = "Index", 
     id = UrlParameter.Optional, 
     language = "en", 
     culture = "CA" 
     } 
); 

和AngularJS我的第一個測試路線是這些:

app.config(['$routeProvider', function ($routeProvider) { 
    //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' }); 
}]); 

作爲一個側面說明,除去從ASP routes.MapRoute{language}和傳球的http://localhost:4416/#/contact定期URL的工作,但沒有任何本地化處理。因此,通常我會使用的是URL中的語言路由(它始終存在並且將在哈希標記之前找到)。
哦,只是作爲參考,我自己基礎上AngularStart項目,我更新的包是ASP.MVC5,EF6,AngularJS 1.2.23

編輯
我知道我的Global.asax.cs文件被觸發兩次,第一次它從我的網址的語言,因爲我在我的項目屬性MAEE默認地址,但隨後發生的事情是,AngularJS觸發踢,然後重新路由到默認/home因爲否則。我假設我可能不得不把東西角碼內處理的語言,但我需要什麼放不明確給我...

回答

0

一段時間後,我設法得到大它的一部分工作(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和角度)。

相關問題