如何调试使用C#创建的API项目

huangapple go评论64阅读模式
英文:

How can i debug API project created using C#

问题

以下是您要翻译的代码部分:

[Route("api/NMRController/Disposition")]
[HttpPost]
public NMR_Object Disposition(NMR nmr)
{
	try
	{
		Console.WriteLine($"nmr: {JsonConvert.SerializeObject(nmr)}");
		string CorrectiveAction = nmr.CorrectiveAction;
		string DispositionResult = nmr.DispositionResult;
		string DispositionComment = nmr.DispositionComment;
		string NoteForGroup = nmr.NoteForGroup;
		string SerialNo = nmr.SerialNo;

		MySql.Data.MySqlClient.MySqlConnection conn;
		conn = new MySql.Data.MySqlClient.MySqlConnection();
		conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr_nmr"].ConnectionString;

		MySql.Data.MySqlClient.MySqlCommand myCommand = conn.CreateCommand();
		myCommand.Connection = conn;
		if (conn.State.ToString() != "Open")
		{
			conn.Open();
		}

		myCommand.CommandText = "UPDATE NMR_SerialNo SET CorrectiveAction = @CorrectiveAction, DispositionResult = @DispositionResult, DispositionComment = @DispositionComment, NoteForGroup = @NoteForGroup WHERE SerialNo = @SerialNo";
		myCommand.Parameters.AddWithValue("@CorrectiveAction", CorrectiveAction);
		myCommand.Parameters.AddWithValue("@DispositionResult", DispositionResult);
		myCommand.Parameters.AddWithValue("@DispositionComment", DispositionComment);
		myCommand.Parameters.AddWithValue("@NoteForGroup", NoteForGroup);
		myCommand.Parameters.AddWithValue("@SerialNo", SerialNo);
		myCommand.ExecuteNonQuery();
		conn.Close();

		// write insert query

		var RM = new NMR_Object();
		RM.data = "";
		RM.success = true;
		// RM = JsonConvert.SerializeObject(dt));
		
		return RM;
	}
	catch (Exception ex)
	{
		var RME = new NMR_Object();
		RME.success = false;
		RME.message = "NMR POST: " + ex.Message;
	}
	return null;
}

这段代码是一个C# API项目的一部分,用于处理POST请求并执行数据库更新操作。如果您需要关于此代码的更多信息或有其他问题,请随时提出。

英文:

I want to debug my C# API project, which is currently in my local.

The reason for debugging is that the function below in that API project returns null for a particular JSON.

[Route("api/NMRController/Disposition")]
[HttpPost]
public NMR_Object Disposition(NMR nmr)
{
	try
	{
		Console.WriteLine($"nmr: {JsonConvert.SerializeObject(nmr)}");
		string CorrectiveAction = nmr.CorrectiveAction;
		string DispositionResult = nmr.DispositionResult;
		string DispositionComment = nmr.DispositionComment;
		string NoteForGroup = nmr.NoteForGroup;
		string SerialNo = nmr.SerialNo;

		MySql.Data.MySqlClient.MySqlConnection conn;
		conn = new MySql.Data.MySqlClient.MySqlConnection();
		conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr_nmr"].ConnectionString;

		MySql.Data.MySqlClient.MySqlCommand myCommand = conn.CreateCommand();
		myCommand.Connection = conn;
		if (conn.State.ToString() != "Open")
		{
			conn.Open();
		}

		myCommand.CommandText = "UPDATE NMR_SerialNo SET CorrectiveAction = @CorrectiveAction, DispositionResult = @DispositionResult, DispositionComment = @DispositionComment, NoteForGroup = @NoteForGroup WHERE SerialNo = @SerialNo";
		myCommand.Parameters.AddWithValue("@CorrectiveAction", CorrectiveAction);
		myCommand.Parameters.AddWithValue("@DispositionResult", DispositionResult);
		myCommand.Parameters.AddWithValue("@DispositionComment", DispositionComment);
		myCommand.Parameters.AddWithValue("@NoteForGroup", NoteForGroup);
		myCommand.Parameters.AddWithValue("@SerialNo", SerialNo);
		myCommand.ExecuteNonQuery();
		conn.Close();

		// write insert query

		var RM = new NMR_Object();
		RM.data = "";
		RM.success = true;
		// RM = JsonConvert.SerializeObject(dt));
		
		return RM;
	}
	catch (Exception ex)
	{
		var RME = new NMR_Object();
		RME.success = false;
		RME.message = "NMR POST: " + ex.Message;
	}
	return null;
}

Here is the JSON that I have passed during the post call:

{
  "CorrectiveAction": "testing",
  "DispositionResult": "testing",
  "DispositionComment": "testing",
  "NoteForGroup": "testing",
  "SerialNo": "MYSQL"
}

答案1

得分: 2

以下是翻译好的部分:

  • It should be possible to debug your API if you run it from visual studio.
  • You can just place a breakpoint in your method that is triggered whenever a request to it is made.
  • You can make a request using a web browser or Postman.
  • From there you should be able to step through the code, inspect variables, etc.
  • This should at least be true for a default ASP.NET project.
  • If you made customizations to how the project is run you might need to do other things to allow debugging.
  • If the breakpoint is not triggered at all there might be some issue with your routing.
  • It might be useful to create a default project just to verify that your test setup works.
英文:

It should be possible to debug your API if you run it from visual studio. You can just place a breakpoint in your method that is triggered whenever a request to it is made. You can make a request using a webbrowser or postman. From there you should be able to step thru the code, inspect variables, etc.

This should at least be true for a default asp.Net project. If you made customizations to how the project is run you might need to do other things to allow debugging.

If the breakpoint is not triggered at all there might be some issue with your routing. It might be useful to create a default project just to verify that your test setup works.

答案2

得分: 1

在Visual Studio for Mac上,打开你的项目。

在左上角,你可以选择是否要使用http或https,然后需要选择调试,以及你想要调试的浏览器。

然后点击运行。

当你的项目成功构建并正在运行时,你可以通过点击代码行旁边的行名来设置断点。

Visual Studio会在进入断点时中断,你可以观察你的变量。

英文:

On Visual Studio for Mac, open your Project.

On the upper left hand side you can choose whether you want to use http or https, then you need to choose debug, and the browser with which you wish to debug.

Then click run

After your project successfully builds and is up and running, you can place breakpoints in visual studio by clicking on the line name next to the line of code you want to test.

Visual studio will break when the breakpoint is entered and you can watch your variables

huangapple
  • 本文由 发表于 2023年4月4日 17:02:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定