質問編集履歴
6
修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -49,4 +49,49 @@ 
     | 
|
| 
       49 
49 
     | 
    
         
             
            追記
         
     | 
| 
       50 
50 
     | 
    
         
             
            OS: Windows10
         
     | 
| 
       51 
51 
     | 
    
         
             
            asp.net core 5
         
     | 
| 
       52 
     | 
    
         
            -
            Visual Studio 2019
         
     | 
| 
      
 52 
     | 
    
         
            +
            Visual Studio 2019
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
            ```C#
         
     | 
| 
      
 56 
     | 
    
         
            +
                    public class Test
         
     | 
| 
      
 57 
     | 
    
         
            +
                    {
         
     | 
| 
      
 58 
     | 
    
         
            +
                        readonly ITestList _testList;
         
     | 
| 
      
 59 
     | 
    
         
            +
                        public Test(ITestList testList)
         
     | 
| 
      
 60 
     | 
    
         
            +
                        {
         
     | 
| 
      
 61 
     | 
    
         
            +
                            _testList = testList;
         
     | 
| 
      
 62 
     | 
    
         
            +
                        }
         
     | 
| 
      
 63 
     | 
    
         
            +
                  //[FormBody]用に追加
         
     | 
| 
      
 64 
     | 
    
         
            +
                        public class Parameter
         
     | 
| 
      
 65 
     | 
    
         
            +
                        {
         
     | 
| 
      
 66 
     | 
    
         
            +
                            public string title { get; set; }
         
     | 
| 
      
 67 
     | 
    
         
            +
                            public string name { get; set; }
         
     | 
| 
      
 68 
     | 
    
         
            +
                        }
         
     | 
| 
      
 69 
     | 
    
         
            +
                        //引数に[FromBody]を追加
         
     | 
| 
      
 70 
     | 
    
         
            +
                        public ServiceResult<TestList> GetList([FromBody] Parameter test)
         
     | 
| 
      
 71 
     | 
    
         
            +
                        {
         
     | 
| 
      
 72 
     | 
    
         
            +
                            ///ここでJsonデータを受け取りたい
         
     | 
| 
      
 73 
     | 
    
         
            +
                            return _testList.getList();
         
     | 
| 
      
 74 
     | 
    
         
            +
                        }
         
     | 
| 
      
 75 
     | 
    
         
            +
                    }
         
     | 
| 
      
 76 
     | 
    
         
            +
            ```
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
            ```javascript
         
     | 
| 
      
 79 
     | 
    
         
            +
               //jsonに名前をMVCのparameterが受け取れるように変更
         
     | 
| 
      
 80 
     | 
    
         
            +
                  const json = {
         
     | 
| 
      
 81 
     | 
    
         
            +
                    title:"test",
         
     | 
| 
      
 82 
     | 
    
         
            +
                    name:"10"
         
     | 
| 
      
 83 
     | 
    
         
            +
                  }
         
     | 
| 
      
 84 
     | 
    
         
            +
             
     | 
| 
      
 85 
     | 
    
         
            +
                  formData.append("files", e.target.files[0])
         
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
      
 87 
     | 
    
         
            +
                  const response = fetch("対象URL",
         
     | 
| 
      
 88 
     | 
    
         
            +
                  {
         
     | 
| 
      
 89 
     | 
    
         
            +
                    method:"post",
         
     | 
| 
      
 90 
     | 
    
         
            +
                    headers:{
         
     | 
| 
      
 91 
     | 
    
         
            +
                      'Content-Type': 'application/json'
         
     | 
| 
      
 92 
     | 
    
         
            +
                    },
         
     | 
| 
      
 93 
     | 
    
         
            +
                    body:JSON.stringify(json)
         
     | 
| 
      
 94 
     | 
    
         
            +
                  })
         
     | 
| 
      
 95 
     | 
    
         
            +
            ```
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
            ご指摘があったのでMVCの方の引数に[FormBody]を追加したところ、デバッグでブレークポイントに止まらなくなりました。
         
     | 
5
細かい修正
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -1,8 +1,8 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            C# asp.net core でweb 
     | 
| 
      
 1 
     | 
    
         
            +
            C# asp.net core でMVC内でweb apiを作成し
         
     | 
| 
       2 
2 
     | 
    
         
             
            そのURLに対してjs のfetch でbodyにjsonを指定して
         
     | 
| 
       3 
3 
     | 
    
         
             
            投げた際にjsonのデータが取得できません
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
     | 
    
         
            -
            web apiの方はvisual studioのデバッグでローカルサーバーをたて
         
     | 
| 
      
 5 
     | 
    
         
            +
            MVC内でweb apiをの方はvisual studioのデバッグでローカルサーバーをたて
         
     | 
| 
       6 
6 
     | 
    
         
             
            ブレークポイントを設定すると止まるので通信は
         
     | 
| 
       7 
7 
     | 
    
         
             
            できていると思うのですが、Request.Body;の中身を見ても
         
     | 
| 
       8 
8 
     | 
    
         
             
            飛ばしたjsonの値がなく困っています。
         
     | 
| 
         @@ -10,11 +10,21 @@ 
     | 
|
| 
       10 
10 
     | 
    
         | 
| 
       11 
11 
     | 
    
         | 
| 
       12 
12 
     | 
    
         
             
            ```C#
         
     | 
| 
      
 13 
     | 
    
         
            +
            	public class Test
         
     | 
| 
      
 14 
     | 
    
         
            +
                {
         
     | 
| 
      
 15 
     | 
    
         
            +
            		readonly ITestList _testList;
         
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
      
 16 
     | 
    
         
            +
            		public Test(ITestList testList)
         
     | 
| 
       14 
17 
     | 
    
         
             
                    {
         
     | 
| 
       15 
     | 
    
         
            -
             
     | 
| 
      
 18 
     | 
    
         
            +
            			_testList = testList;
         
     | 
| 
       16 
     | 
    
         
            -
                        return View();  
         
     | 
| 
       17 
19 
     | 
    
         
             
                    }
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
            		public ServiceResult<TestList> GetList()
         
     | 
| 
      
 22 
     | 
    
         
            +
                    {
         
     | 
| 
      
 23 
     | 
    
         
            +
            			///ここでJsonデータを受け取りたい
         
     | 
| 
      
 24 
     | 
    
         
            +
            			return _testList.getList();
         
     | 
| 
      
 25 
     | 
    
         
            +
                    }
         
     | 
| 
      
 26 
     | 
    
         
            +
                }
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
       18 
28 
     | 
    
         
             
            ```
         
     | 
| 
       19 
29 
     | 
    
         | 
| 
       20 
30 
     | 
    
         
             
            ```javascript
         
     | 
4
ツールの追加
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -38,4 +38,5 @@ 
     | 
|
| 
       38 
38 
     | 
    
         | 
| 
       39 
39 
     | 
    
         
             
            追記
         
     | 
| 
       40 
40 
     | 
    
         
             
            OS: Windows10
         
     | 
| 
       41 
     | 
    
         
            -
            asp.net core 5
         
     | 
| 
      
 41 
     | 
    
         
            +
            asp.net core 5
         
     | 
| 
      
 42 
     | 
    
         
            +
            Visual Studio 2019
         
     | 
3
タグの追加
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         
            File without changes
         
     | 
2
os,フレームワークの追記
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -33,4 +33,9 @@ 
     | 
|
| 
       33 
33 
     | 
    
         
             
                    },
         
     | 
| 
       34 
34 
     | 
    
         
             
                    body:JSON.stringify(json)
         
     | 
| 
       35 
35 
     | 
    
         
             
                  })
         
     | 
| 
       36 
     | 
    
         
            -
            ```
         
     | 
| 
      
 36 
     | 
    
         
            +
            ```
         
     | 
| 
      
 37 
     | 
    
         
            +
             
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
            追記
         
     | 
| 
      
 40 
     | 
    
         
            +
            OS: Windows10
         
     | 
| 
      
 41 
     | 
    
         
            +
            asp.net core 5
         
     | 
1
タイトル
    
        title	
    CHANGED
    
    | 
         @@ -1,1 +1,1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            jsonデータをweb apiで取得したい
         
     | 
| 
      
 1 
     | 
    
         
            +
            jsonデータをASP.net web apiで取得したい
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -8,6 +8,7 @@ 
     | 
|
| 
       8 
8 
     | 
    
         
             
            飛ばしたjsonの値がなく困っています。
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
       11 
12 
     | 
    
         
             
            ```C#
         
     | 
| 
       12 
13 
     | 
    
         
             
                    public IActionResult Index()
         
     | 
| 
       13 
14 
     | 
    
         
             
                    {
         
     |