阅读 126

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(new UserServer());
            services.AddGrpcClient(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";
}

class="text-center">

class="display-4">Welcome

Learn about "https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core.

"showApiData">
"showGrpcData">

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

原文:https://www.cnblogs.com/lhwpc/p/15191261.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐