传值

作者: 程序员潜规则 | 来源:发表于2020-04-03 01:01 被阅读0次

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using Microsoft.AspNetCore.Mvc.Filters;
namespace Test
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

    readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        // cors
        services.AddCors(options =>
        {
            options.AddPolicy("AllowAll",
            builder =>
            {
                builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            });
        });

        

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        // cors
        app.UseCors("AllowAll"); 
        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Net;

namespace Test.Controllers
{
[ApiController]
[Route("api/[controller]")]
//[Produces(MediaTypeNames.Application.Json)]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

    public People peo = new People {Pid = 123, PName = "sunjj"};
    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public ActionResult Get()
    {
        return Ok("name");
    }

    [HttpGet("GetTestData/{id}")]
    public ActionResult<object> GetTwo(string id)
    {
        return Ok(new {
            value = id
        });
    }

    [HttpGet("GetThree")]
    public ActionResult<object> GetThree([FromQuery] int uid, [FromQuery] string name)
    {
        
        return Ok(new {
            id = uid,
            name = name
        });
    }

    [HttpGet("GetObject")]
    public People GetPeopleInfo()
    {
        return new People { Pid= 123, PName = "sunjj" };
    }

    [HttpPost("PostMethod")]
    public ActionResult<object> PostMethod([FromBody] UserInfo user)
    {
        return Ok(user);
    }

    [HttpPut("PutMethod")]
    public ActionResult<object> PutMethod([FromBody] UserInfo user)
    {
        return Ok(user);
    }

    
}

}
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';

import { UserInfo } from 'src/Hero';
import { userInfo } from 'os';
import { join } from 'path';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
heroesUrl = 'https://localhost:5001/api/weatherforecast/GetThree';
herosPostUrl = 'https://localhost:5001/api/weatherforecast/PostMethod';
heroPutUrl = 'https://localhost:5001/api/weatherforecast/PutMethod';

title: string = 'my hero App';

// httpOptions = {
// headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
// observe:'response',
// params: new HttpParams({
// uid: '1234',
// name: ""
// })
// };

constructor(private http: HttpClient) { }

ngOnInit() {

//this.getHttpInstance();
//this.postHttpInstance();

this.putMethod();

}

private putMethod() {
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'application/json');
let body: any = JSON.stringify({ id: 20, name: 'Tornado', sex: 1, telephone: '123456789' });
this.http.put<any>(this.heroPutUrl, body, { headers, observe: 'response' }).subscribe(data => {
if (data.status === 200) {
alert(data.body.telephone);
}
});
}

private postHttpInstance() {
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'application/json');
let body: any = JSON.stringify({ id: 20, name: 'Tornado', sex: 1, telephone: '123456789' });
this.http.post<any>(this.herosPostUrl, body, { headers, observe: 'response' }).subscribe(data => {
if (data.status === 200) {
alert(data.body.name);
}
});
}

private getHttpInstance() {
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'application/json');
let params = new HttpParams();
params = params.append('uid', '1001');
params = params.append('name', 'sun');
this.http.get<any>(this.heroesUrl, { headers, observe: 'body', params }).subscribe(data => {
// if (data.status === 200) {
// alert(data.body.id);
// alert(data.body.name);
// }
if (data.id === 1001) {
alert(data.id);
}
else {
alert(data.id);
}
});
}
}

相关文章

  • iOS的五种传值

    前言 iOS常见的五种传值分别为属性传值,通知传值,代理传值,block传值,单例传值 属性传值 用于正向传值,简...

  • 页面传值-03

    一、传值分类 页面传值基本分为两种:正向传值和反向传值。 二、传值方式 传值,最基本的无非就是代理传值、通知传值、...

  • iOS 页面(代理、通知、block、单例、属性)传值

    一、传值分类 页面传值基本分为两种:正向传值和反向传值。 二、传值方式 传值,最基本的无非就是代理传值、通知传值、...

  • iOS-传值方式

    传值方式:1、属性传值 方法传值2、代理传值3、单例传值 4、通知传值 NSNotificationCente...

  • Swift常用的界面传值(属性传值、协议传值、闭包传值)

    1、属性传值 属性传值多用于正向传值(A->B) 2、代理传值 代理传值多用于反向传值(B->A) 3、闭包传值 ...

  • Vue组件间关系及六种传值方式

    前言: 六种传值方式为: 属性传值 $refs $parent 通知传值(广播传值) 本地传值 路由传值 在介绍组...

  • 【iOS开发细节】之- delegate代理的使用

    在iOS开发中、好多时候需要涉及到页面传值、而传值又分为正向传值和反向传值 一、 传值 1、正向传值 2、反向传值...

  • reactNative 之组件传值和反向传值

    在项目中我们经常会遇到传值,传值有正向传值和反向传值,比如1.正向传值:从A组件push到B组件传值2.反向传值:...

  • 代理

    不同页面间传值是必不可少,传值的方式有很多(方法传值,属性传值,代理传值,单例传值) ,这里主要总结下属性传值和代...

  • ios常用的三种传值方式

    iOS中有多种方案可以实现页面之间的传值,例如:属性传值、代理传值、block传值、单例传值...。常用的三种传值...

网友评论

      本文标题:传值

      本文链接:https://www.haomeiwen.com/subject/fsgqphtx.html