FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build
WORKDIR /src
COPY ["dotnetapp-discover.csproj", "dotnetapp-discover/"]
RUN dotnet restore "dotnetapp-discover/dotnetapp-discover.csproj"
COPY . ./dotnetapp-discover
WORKDIR "/src/dotnetapp-discover"
RUN dotnet build "dotnetapp-discover.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "dotnetapp-discover.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "dotnetapp-discover.dll"]
Access to XMLHttpRequest at 'https://dev.azurewebsites.net:5001/api/Auth/app-login' from origin 'https://dev.azurewebsites.net:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
代表被preflight預檢請求擋掉了
這個時候在Api的Program.cs
var app = builder.Build();
//之後加上
app.UseCors(options =>
options
.WithOrigins("https://dev.azurewebsites.net:3000", "*")
//note Allow Any Origin或直接"*"不會過
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
//之前加上
app.UseHttpsRedirection();
註2∶若你的store procedure中有使用到External DataSource,語法中有使用With (nolock)的話,會出現 – “Table hints are not supported on queries that reference external tables”.的錯誤訊息
using System;
public class Program
{
public struct Point
{
public double x;
public double y;
}
public class Shape
{
public Shape(string shapeName)
{
ShapeName = shapeName;
}
public string ShapeName;
public static void DrawShape(Shape t)
{
if (t.ShapeName == "circle")
{
((Circle)t).Draw();
}
if (t.ShapeName == "square")
{
(t as Square).Draw();
}
}
}
public class Circle : Shape
{
public Point center;
public double radius;
public Circle(string typeName):base(typeName)
{
}
public void Draw()
{
Console.WriteLine("this is circle");
}
}
public class Square : Shape
{
public Point TopLeft;
public double side;
public Square(string typeName):base(typeName)
{
}
public void Draw()
{
Console.WriteLine("this is square");
}
}
public static void Main()
{
//註:這邊更好是用列舉的方式指定Name, 強制約定
Shape a = new Circle("circle");
Shape b = new Square("square");
Shape.DrawShape(a);
Shape.DrawShape(b);
}
}
using System;
public class Program
{
public class Rectangle
{
protected double width;
protected double height;
public virtual double Height
{
get
{
return height;
}
set
{
height = value;
}
}
public virtual double Width
{
get
{
return width;
}
set
{
width = value;
}
}
public double Area()
{
return width*height;
}
}
public class Square :Rectangle
{
public override double Height
{
get
{
return base.height;
}
set
{
base.width = value;
base.height = value;
}
}
public override double Width
{
get
{
return base.width;
}
set
{
base.height = value;
base.width = value;
}
}
}
public static void Main()
{
Rectangle r = new Square();
r.Width = 5;
r.Height = 4;
//對正方向設定5再設定4的邊界,行為意圖比較像是拉動邊的長短而己
Console.WriteLine("square area is "+r.Area());
}
}
Rectangle r = new Square();
var result = CalculateArea(r);
public static double CalculateArea(Rectangle r)
{
r.Width = 4;
r.Height = 5;
if (r.Area() != 20)
throw new Exception("bad area");
return r.Area();
}