zoukankan      html  css  js  c++  java
  • Asp.Net Core MVC客户端调用 Asp.Net Core Web Api ,Api再调用Grpc服务

    1、沿用上一篇的Asp.Net Core Web API服务,修改Startup.cs,解决跨域问题

      public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            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();
                services.AddSwaggerGen(c =>
                {
                    c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApiService", Version = "v1" });
                    c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                });
                services.AddSingleton<UserServer>(new UserServer());
                services.AddGrpcClient<TestGrpc.TestGrpcClient>(options =>
                {
                    options.Address = new Uri("http://localhost:5000");
                });
    
                services.AddCors(options =>
                 {
                     options.AddPolicy("myAllows", policys =>
                     {
                         policys.AllowAnyHeader();
                         policys.AllowAnyMethod();
                         policys.AllowCredentials();
                         policys.WithOrigins(new[] { "http://localhost:5005" });
                         }) ;
                 }
                );
    
            }
    
            // 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();
                    app.UseSwagger();
                    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApiService v1"));
                }
    
                app.UseRouting();
                app.UseCors("myAllows");
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }

    2、新建Asp.Net Core Web MVC项目

    3、修改launchSettings.json

    {
     
      "profiles": {
            "WebApplication1": {
          "commandName": "Project",
          "dotnetRunMessages": "true",
          "launchBrowser": true,
          "applicationUrl": "http://localhost:5005",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }

    4、修改Index.cshtml

    @{
        ViewData["Title"] = "Home Page";
    }
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    </div>
    <div id="showApiData">
    
    </div>
    <div id="showGrpcData">
    
    </div>
    
    <script type="text/javascript">
        $(function () {
            $.ajax({
                url: "http://localhost:5007/api/MyApi/Get",
                type: "Get",
                success: function (data) {
                    var str = "";
                    alert(JSON.stringify(data));
                    for (var i = 0; i < data.length; i++) {
    
                        str += data[i].name;
                    }
                    $("#showApiData").html(str);
                }
            })
    
            $.ajax({
                url: "http://localhost:5007/api/MyApi/GetGrpc",
                type: "Get",
                success: function (data) {
                    var str = "";
                    alert(JSON.stringify(data));
                    str = data;
                    $("#showGrpcData").html(str);
                }
            })
    
        })
    
    
    </script>

    5、启动Grpc 服务,启动Asp.Net Core Web API 服务,启动MVC项目。

  • 相关阅读:
    Beta冲刺 第二天
    Beta冲刺 第一天
    实验十一 团队项目设计完善&编码测试
    实验十 软件系统详细设计与概要设计的改进
    实验九 FBG 团队项目需求改进与系统设计
    实验八 <FBG> 基于原型的团队项目需求调研与分析
    实验七 《FBG》—-小学生课后习题答案原型设计
    实验五 <FBG>团队亮相
    Windows zip版本安装MySQL
    redis安装与简单实用
  • 原文地址:https://www.cnblogs.com/lhwpc/p/15191261.html
Copyright © 2011-2022 走看看