简体   繁体   English

如何使用c#asp.net从URL获取数据?

[英]How can I use c# asp.net to get data from url?

I'm trying to get some info I sent by form to angularJS in my c# asp.net backend, and I'm having trouble doing it. 我正在尝试获取一些信息,这些信息是通过表单发送到我的c#asp.net后端中的angularJS的,我在这样做时遇到了麻烦。

Visual Studio won't let me compile because it says: Visual Studio不允许我编译,因为它说:

Error CS0120: an object reference is required for the nonstatic field, method, or property 'member' 错误CS0120:非静态字段,方法或属性“成员”需要对象引用

That's is my controller 那是我的控制器

public class SearchController : ApiController
{
    public string Get()
    {
        string test = HttpContext.Request.QueryString["txt_search"];
        return test;
    }

}

Here's what I got in my angularjs: 这是我在angularjs中得到的:

$scope.sendForm = function () {
    console.log($scope.model.search);
    $http({
        method: 'GET',
        url: '/api/search?txt_search=' + $scope.model.search
    })
    .then(function () {
        console.log('sucesso a executar o post');
    }, function () {
           console.log('Erro ao executar o post');
       });
};

As suggested in the comments, you should just be able to change your method definition and skip this altogether: 如注释中所建议,您应该只能够更改方法定义并完全跳过此方法:

public string Get(string txt_search)
{
    return txt_search;
}

Alternatively, to reference the current request, I believe you need to use the following (note the addition of .Current ): 另外,要引用当前请求,我相信您需要使用以下内容(请注意添加.Current ):

string test = HttpContext.Current.Request.QueryString["txt_search"];

The reason is that HttpContext defines Request as an instance property. 原因是HttpContextRequest定义为实例属性。 The only public static property is Current , which returns an instance of HttpContext through which you can reach Request . 唯一的公共静态属性是Current ,它返回HttpContext的实例,您可以通过该实例到达Request

Welcome to Stack Overflow, 欢迎使用Stack Overflow,

Your Angular code is correct 您的Angular代码正确

You need to pass a parameter on server side to collect txt_search value 您需要在服务器端传递参数以收集txt_search

Here it is: 这里是:

[HttpGet]
[Route("/api/search")]
public string mySearchMethod(string txt_search)
{
    //something here with txt_search
    return "OK";
}

Both of the above solution will work, but for another approach as you are using asp.net web api and router you can make it as below as well 上述两种解决方案都可以使用,但是对于使用asp.net Web API和路由器的另一种方法,您也可以按照以下方法进行操作

In your Angular code, simple pass the search as below 在您的Angular代码中,简单地通过搜索,如下所示

``` ```

$scope.sendForm = function () {
    console.log($scope.model.search);
    $http({
        method: 'GET',
        url: '/api/search/'+ $scope.model.search
    })
    .then(function () {
        console.log('sucesso a executar o post');
    }, function () {
           console.log('Erro ao executar o post');
       });
};

``` ```

Notice url: '/api/search/'+ $scope.model.search 通知url: '/api/search/'+ $scope.model.search

and change the Action method as below 并如下更改Action方法

``` ```

[HttpGet]
[Route("/api/search/{txt_search}")]
public string mySearchMethod(string txt_search)
{
    //something here with txt_search
    return "OK";
}

``` ```

by doing this you don't have to worry about the name of the parameter txt_search whatever you mention in route [Route("/api/search/{txt_search}")] , you will get the value in same parameter. 通过这样做,您不必担心在路由[Route("/api/search/{txt_search}")]提及的参数txt_search的名称,您将获得同一参数中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM