【问题标题】:HttpContext.Current.Request with Angular ui-router带有 Angular ui-router 的 HttpContext.Current.Request
【发布时间】:2014-10-01 12:26:53
【问题描述】:

使用 Asp.Net,我在加载时捕获了 AngularJS 路由的请求。路线是/FST/#/quickstart。我正在尝试通过以下方式捕获该路线:

var currentUrl = HttpContext.Current.Request.RawUrl;

但是,RawUrl 不包含 #/quickstart 子部分。请求中似乎没有任何东西可以告诉我完整的 URL。如何获取目标网址?

【问题讨论】:

  • 您不能将 URL 中的# 传递给服务器。
  • what is the # symbol in the url 的可能重复项
  • 当我们使用 angularJs 开发时,在服务器端获取哈希部分是没有意义的。客户端的哈希should be used to do routing 可以将解析的参数(如果有)发送到服务器端。

标签: c# angularjs asp.net-web-api


【解决方案1】:

您可以使用$http 拦截器将当前路由作为标头添加到您的 Web api 请求中

  $httpProvider.interceptors.push(['$q','$location' function($q, $location) {
        return {
            'request': function(config) {
                config.headers.ClientSideUrl = $location.path();
                return config || $q.when(config);
            },
            'requestError': function(rejection) {
                return $q.reject(rejection);
            },
            'response': function(response) {
                return response || $q.when(response);
            },
            'responseError': function(rejection) {
                return $q.reject(rejection);
            }
        };

    }]);

然后访问header服务器端:

HttpContext.Current.Request.Headers.GetValues("ClientSideUrl").FirstOrDefault();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多