我正在尝试使用 ASP.NET Core MVC 构建一个简单的 API。
我已经成功构建了一个具有获取、发布、编辑和删除功能的 api。然而,Postman JSON 原始帖子会产生 Null 值,但如果我使用表单数据,它会完美运行。
我想以原始 json 形式发送数据,知道我做错了什么吗?
我的模特
[Table("continous value monitoring")]
public class Continous
{
[Key]
public int UserID { get; set; }
public float Value { get; set; }
public string ReadingType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
数据上下文:
public class DataContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionBuilder)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var configuration = builder.Build();
optionBuilder.UseMySql(configuration
["ConnectionString:DefaultConnection"]);
}
public DbSet<Continous> Continous { get; set; } …Run Code Online (Sandbox Code Playgroud) 这是我的连接字符串
{
"Data": {
"PhotoGalleryConnection": {
"ConnectionString": "Server=WINDOWS-B7MJR5T\\SQLEXPRESS;User Id=sa;password=allah;Database=PhotoGallery;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正面临这个例外
Microsoft.EntityFrameworkCore.dll 中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理附加信息:无法打开登录请求的数据库“PhotoGallery”。登录失败。
在 ASP.NET Core 2.1 Web 应用程序中,如何在按下后退按钮后刷新页面并加载新页面?
我有两个微服务。第一项服务为客户,第二项服务为发票服务。
在 Invoice MicroService 中,我将仅保存 CustomerId。使用此 CustomerId,我想检索客户微服务的所有相关数据。但我不知道异步通信。
请给我一些想法。
c# asp.net-core-mvc microservices asp.net-core asp.net-core-webapi
我有一个用 C# 编写的 ASP.NET Core 3.1 MVC 项目,它使用 SQL Server 数据库。我有连接字符串appsettings.json并将其设置为我的本地数据库。
我的问题是,当我发布应用程序时,SQL Server 将具有不同的名称。如何获得用于开发和生产的连接字符串?
我已经在互联网上搜索了好几天并尝试了各种方法,但似乎没有任何效果。
非常感谢您的帮助,
目前,我正在使用Microsoft MVC框架开发Web API。在他们的文档中,我阅读了以下内容(https://docs.microsoft.com/zh-cn/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1):
为方便起见,属性路由通过将令牌括在方括号([,])中来支持令牌替换。标记[action],[area]和[controller]将被定义路径的动作中的动作名称,区域名称和控制器名称的值替换。在此示例中,操作可以匹配注释中所述的URL路径:
[Route("[controller]/[action]")]
public class ProductsController : Controller
{
[HttpGet] // Matches '/Products/List'
public IActionResult List() {
// ...
}
[HttpGet("{id}")] // Matches '/Products/Edit/{id}'
public IActionResult Edit(int id) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
令牌替换是构建属性路由的最后一步。上面的示例的行为与以下代码相同:
public class ProductsController : Controller
{
[HttpGet("[controller]/[action]")] // Matches '/Products/List'
public IActionResult List() {
// ...
}
[HttpGet("[controller]/[action]/{id}")] // Matches '/Products/Edit/{id}'
public IActionResult Edit(int id) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
但是,每当我尝试使用该[HttpGet("my/route")]属性时,Visual Studio都会不断告诉我“ HttpGetAttribute不包含带有1个参数的构造函数”。我已经读过我应该Microsoft.AspNet.WebApi.WebHost使用软件包管理器进行安装,但是错误仍然存在。
我的问题是,我如何才能开始使用适当的属性?我没有在Visual Studio中安装软件包的经验。
感谢您的协助。
这是我的appsettings.json。它位于没有startup.cs的类库中。我想知道是否有可能从那里获取变量以在本地使用?
{
"AccountsAddress": "http://localhost:55260/api/Accounts/",
"ApplicationUsersAddress": "http://localhost:55260/api/ApplicationUsers/"
}
public static class Manager
{
public static IConfiguration AppSetting { get; }
static Manager()
{
AppSetting = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我图书馆的结构
我使用dotnet cli创建了一个项目,一切正常,直到我想为应用程序编写测试.
我创建了一个src目录并将整个Web项目移动到该src文件夹,之后我在一个名为的文件夹中创建了测试项目tests.
我在测试项目中添加了对Web应用程序的引用,测试成功运行但是当我运行我的应用程序时,dotnet run我收到以下错误:
该
Views文件夹位于src目录中,其他所有文件都包含*.csproj.
请在下面找到我的文件夹结构:
任何帮助,将不胜感激.谢谢.
我刚刚开始进入netcore 2.2,因此尝试从角度调用控制器,但是当我从服务中调用发送联系人电子邮件方法时,会收到“ http故障响应:http:// localhost:53270 / api / DataController / SendContactEmail /:找不到404”
如您所见,我在下面的例程中明确写出了路由
sendContactEmail(contact: ISubmitModel): Observable<any> {
return this._http.post<any>('api/DataController/SendContactEmail/', contact)
}
Run Code Online (Sandbox Code Playgroud)
和我的控制器方法
[Route("api/[controller]")]
public class DataController : Controller
{
[HttpPost("[action]")]
public IActionResult SendContactEmail(ContactUs contact)
{
ContactUs form = new ContactUs()
{
Email = contact.Email,
Name = contact.Name,
Phone = contact.Phone,
Message = contact.Message
};
return Ok(form);
}
public class ContactUs
{
public string Name { get; set; }
public string Phone { get; set; }
public string Email { …Run Code Online (Sandbox Code Playgroud) 我创建了自己的用户管理类来调用UserManager.我希望控制器能够访问UserManager对象以登录或注册用户.
我知道我必须在控制器类中提供一个参数化构造函数,它接受一个对象UserManager并将其赋值给私有属性等.
但是我在哪里以及如何在我的项目中注册我的类,以便它将由ASP.NET MVC Core框架自动注入?