WebGL 版本中的 HTTP 500 内部服务器错误

HTTP 500 Internal Server Error in WebGL version

我有自己的 ASP.net Web API 2 服务器 Entity Framework,托管在 Azure 上。它连接来自 Azure 的数据库并为 GET 和 POST 请求创建控制器。还创建了 DTO。 Unity 上还有一个应用程序,它连接到服务器并以关于各种条件的日志的形式向他发送请求。在独立版本中,一切正常,但如果您在 WebGL 版本中执行此操作,并将其放在主机上,则会出现(500 内部服务器错误)。而且我不知道可能是什么问题

在服务器上获取:

[ResponseType(typeof(Rooms))]
    public IHttpActionResult GetRoom(string id)
    {
        Rooms room = db.Rooms.Where(r => r.RoomName == id).FirstOrDefault();
        var roomdto = new RoomDto(room);
        if (roomdto == null)
        {
            return NotFound();
        }

        return Ok(roomdto);
    }

DTO 示例:

    public class RoomDto
{
    public int IDRoom { get; set; }
    public string RoomName { get; set; }

    public RoomDto(Rooms entity)
    {
        IDRoom = entity.IDRoom;
        RoomName = entity.RoomName;
    }
}

我的数据库:

   public partial class LogsDBModel : DbContext
{
    public LogsDBModel()
        : base("name=DBModel")
    {
    }

    public virtual DbSet<Ball> Ball { get; set; }
    public virtual DbSet<DirectionVector> DirectionVector { get; set; }
    public virtual DbSet<GameTime> GameTime { get; set; }
    public virtual DbSet<Impact> Impact { get; set; }
    public virtual DbSet<Players> Players { get; set; }
    public virtual DbSet<Rooms> Rooms { get; set; }
    public virtual DbSet<database_firewall_rules> database_firewall_rules { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<GameTime>()
            .HasMany(e => e.Ball)
            .WithOptional(e => e.GameTime)
            .WillCascadeOnDelete();

        modelBuilder.Entity<Players>()
            .Property(e => e.PlayerName)
            .IsFixedLength();

        modelBuilder.Entity<Rooms>()
            .Property(e => e.RoomName)
            .IsFixedLength();

        modelBuilder.Entity<Rooms>()
            .HasMany(e => e.GameTime)
            .WithOptional(e => e.Rooms)
            .WillCascadeOnDelete();

        modelBuilder.Entity<Rooms>()
            .HasMany(e => e.Players1)
            .WithOptional(e => e.Rooms)
            .WillCascadeOnDelete();

        modelBuilder.Entity<database_firewall_rules>()
            .Property(e => e.start_ip_address)
            .IsUnicode(false);

        modelBuilder.Entity<database_firewall_rules>()
            .Property(e => e.end_ip_address)
            .IsUnicode(false);
    }
}

从 Unity 获取:

     public IEnumerator GetRequest(string uri)
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(Host + uri))
        {
            yield return webRequest.SendWebRequest();
            response = webRequest.downloadHandler.text;
        }
    }
    public IEnumerator GetID(string entity, string identify)
{
    yield return StartCoroutine(GetRequest(entity + identify));
    switch(entity)
    {
        case "Rooms/":
            Rooms room = Rooms.CreateFromJSON(response);
            ID = room.IDRoom;
            break;
        case "Players/":
            Players player = Players.CreateFromJSON(response);
            ID = player.IDPlayer;
            break;
        case "DirectionVectors/":
            DirectionVectors dv = DirectionVectors.CreateFromJSON(response);
            ID = dv.IDDirectionVector;
            break;
        case "GameTimes/":
            GameTimes gt = GameTimes.CreateFromJSON(response);
            ID = gt.IDGameTime;
            break;
    }
}

Json:

[System.Serializable]
public class Rooms
{
    public int IDRoom;
    [System.NonSerialized]
    public string RoomName;
    public static Rooms CreateFromJSON(string jsonString)
    {
        return JsonUtility.FromJson<Rooms>(jsonString);
    }
}

{"Message":"An error has occurred.","ExceptionMessage":"The specified policy origin 'https://newlogsserverapi20200402121056.azurewebsites.net/' is invalid. It must not contain a path, query, or fragment.","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Cors.EnableCorsAttribute.ValidateOrigins(IList`1 origins)\r\n at System.Web.Http.Cors.EnableCorsAttribute.GetCorsPolicyAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Cors.CorsMessageHandler.d__8.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Cors.CorsMessageHandler.d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Cors.CorsMessageHandler.d__4.MoveNext()"}

指定 CROS 时错误地址错误

var cors = new EnableCorsAttribute("https://newlogsserverapi20200402121056.azurewebsites.net", "*", "*");
        config.EnableCors(cors);
  1. 您的 WebGL 构建文件可能太大。(这意味着您的游戏项目太大)要验证是否是这种情况,请输入 您在此页面上的 zip 文件:The upload game webpage (最大 大小为 100MB)

  2. 要访问 上传游戏网页 ,转到 play.unity.com 并按一个按钮 在导航栏上看起来像这样:打开时要按的按钮 play.unity.com

  3. 这里有一段视频演示了一种简单但有效的方法来减少游戏项目的大小:youtu.be/7O21c8BzEzM

这是我以前遇到的问题,希望对遇到类似困境的人有所帮助。