目录

C-后端入门一

C# 后端入门(一)

最近打算编写服务器,客户端用U3D,后端语言打算用C# ,本文为后端入门系列文章,记录一下踩坑过程

目录

创建C# WebApi 工程,

https://i-blog.csdnimg.cn/direct/95ff5069cf214579adaba6b925618a6a.png#pic_center

https://i-blog.csdnimg.cn/direct/f9bfc696cd664b45b63b7acdf00f98e1.png#pic_center

取消Https勾选

https://i-blog.csdnimg.cn/direct/bdeff47d260543a0b0999eea606ad421.png#pic_center

这样项目创建成功

项目目录结构

这是新建的项目结构,重点关注Controllers.当我们选择webAPI 创建项目的时候,它已经帮我们创建一个demo,我们直接运行看看

https://i-blog.csdnimg.cn/direct/cede58b2f08f41eeb14e0c91a6fe8505.png#pic_center

运行后,会自动打开Swagger,在里面可以看到,里面定义了一个WeatherForecast接口,执行一下,成功接口成功返回信息

https://i-blog.csdnimg.cn/direct/91228e563eae4c6587c9f71a076c0821.png#pic_center

创建自己的接口

在上面创建项目里,WeatherForecastController 就是定义接口的脚本

using Microsoft.AspNetCore.Mvc;

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

        private readonly ILogger<WeatherForecastController> _logger;

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

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }

}

我们仿照这个脚本创建自己的接口,在 Controllers 文件夹下创建 MyTestController 控制器,右键 Controllers =》添加=》=》控制器

https://i-blog.csdnimg.cn/direct/9ca2cd440d24483b8ae76d6f63a762d3.png#pic_center

添加后名称修改 MyTestController,然后对脚本作一下修改,如下

using Microsoft.AspNetCore.Mvc;

namespace MyWebApi.Controllers
{
[Route("api/[controller]/[Action]")]
[ApiController]
public class MyTestController : ControllerBase
{  
 [HttpGet]

        public string TestHellWorld()
        {
            return "hell World!";
        }

        [HttpGet]
        public string TestFunction_2(string name, string msg)
        {
            return $"{name}:{msg}";
        }
    }

}

[HttpGet] 特性 定义改接口是 GET 方法访问

Route(“api/[controller/Action”)] 特性,这里其实是定义路由地址,也就是 API 的访问地址

return 这里的就是返回改接口的数据

我们来运行一下项目

https://i-blog.csdnimg.cn/direct/2bbf0c9f004b4060976353ce3b429751.png#pic_center

这里看见 Swagger 已经能看见刚刚我们自己定义的接口 TestHellWorld 和 TestFunction_2,然后我们用 Swagger 请求一下接口,看看数据返回

https://i-blog.csdnimg.cn/direct/3838c8b90f2b4c318c57e9f21edb6045.png#pic_center

数据返回 hell World! 没问题

创建 U3D 工程,请求接口

U3D 这边简单带过,创建一个空工程,简单挂一个请求接口代码运行就行了,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class TestGet : MonoBehaviour
{
const string url_1 = "http://localhost:5107/api/MyTest/TestHellWorld";
// Start is called before the first frame update
void Start()
{
StartCoroutine(RequestData_Get(url_1));
}

    IEnumerator RequestData_Get(string url)
    {
        UnityWebRequest webRequest =  UnityWebRequest.Get(url);

        yield return webRequest.SendWebRequest();

        if(webRequest.error != null)
        {
            Debug.Log(webRequest.error);
            Debug.Log($"请求{url}接口出错");
        }
        else
        {
            string data = webRequest.downloadHandler.text;
            Debug.Log(data);
        }
    }

}

请求接口的地址是在 Swagger 上面有标识

https://i-blog.csdnimg.cn/direct/304e1109b40b4217a23515671bd232cf.png#pic_center

运行 U3D 之前,记得运行后端项目,不然 U3D 请求不了接口

接口数据打印出来,没问题,请求成功

https://i-blog.csdnimg.cn/direct/8fef8b0673944090ad68890630180048.png#pic_center

部署到 IIS 服务器上

开启 IIS 服务

win+R =>输入 control => 回车确认

https://i-blog.csdnimg.cn/direct/492bc62ef76b442ea2227f7519269155.png#pic_center

https://i-blog.csdnimg.cn/direct/ebcf3505f96a49329f0d577f7ce109ff.png#pic_center

https://i-blog.csdnimg.cn/direct/b7cec620b2e5431ca7cd303cdade586d.png#pic_center

按上图打勾,这里需要注意,点开 应用程序开发功能

https://i-blog.csdnimg.cn/direct/bb515d6f523e4435b12092eee2bdd20e.png#pic_center

这样配置,然后确认修改就行了。搜索 IIS,打开

https://i-blog.csdnimg.cn/direct/589bd3ea63b84f0a8c698837c4cc8e4d.png#pic_center

发布后端程序

回到 VS 编辑器,右键项目发布,发布完成之后会生成一个文件夹,我们把文件夹部署到 iis 上面

打开 iis => 右键网站 => 添加网站

https://i-blog.csdnimg.cn/direct/cd1b35fbae344f9ea9e70f84001d7181.png#pic_center

物理路径选择 刚刚发布后的文件路径,端口号自定义,我这里是 8090

https://i-blog.csdnimg.cn/direct/0347da70d99440d387a833c8043cd3c4.png#pic_center

点击确认,部署就完成了

部署 U3D

返回到 Unity, 切换到 webGl 平台,打包。

打包成功后,把文件夹部署到 iis 上,具体请参考上面部署后端代码,流程一样.

至此,整个发布的流程就已经完成,下面打开 U3D 网站,就可以请求到接口数据了。

但是运行 U3D 之后,会发现报错和请求不了接口数据的情况,我会在下一篇文章中展示并解决。

链接: