C# 如何对自定义控制器方法执行HTTPGet
对自定义控制器方法GetTestInfoByTestTypeID执行httpget请求的最佳方法是什么。请在下面找到我的客户端应用程序、API控制器、模型和dbcontext。TestInfo和TestInfo/ID请求可以工作,但是getTestInfo-ByTestTypeId方法不工作 客户端C# 如何对自定义控制器方法执行HTTPGet,c#,entity-framework,model-view-controller,httprequest,dbcontext,C#,Entity Framework,Model View Controller,Httprequest,Dbcontext,对自定义控制器方法GetTestInfoByTestTypeID执行httpget请求的最佳方法是什么。请在下面找到我的客户端应用程序、API控制器、模型和dbcontext。TestInfo和TestInfo/ID请求可以工作,但是getTestInfo-ByTestTypeId方法不工作 客户端 request.Method = HttpMethod.Get; // request.RequestUri = new Uri(ConstantsCS.c_BaseURL + "/Te
request.Method = HttpMethod.Get;
// request.RequestUri = new Uri(ConstantsCS.c_BaseURL + "/TestIT/TestInfo"); WORKS
// request.RequestUri = new Uri(ConstantsCS.c_BaseURL + "/TestIT/TestInfo/4"); WORKS
request.RequestUri = new Uri(ConstantsCS.c_BaseURL + "/TestIT/TestInfo/GetTestInfoByTestTypeID/4"); //DOES NOT WORK
HttpResponseMessage responsemsg = await client.SendAsync(request);
if (responsemsg.StatusCode == System.Net.HttpStatusCode.OK)
{
}
API控制器方法:
#region "TestIT/Testinfo/GetTestInfoByTestTypeID/1"
[HttpGet("{TestTypeId}", Name = "GetTestInfoByTestTypeID")]
public IEnumerable<TestInfo> GetTestInfoByTestTypeID([FromRoute] int TestTypeId)
{
try
{
List<TestInfo> tstinfoIn = _context.TestInfo.ToList();
List<TestTypeInfo> tsttypinfoIn = _context.TestTypeInfo.ToList();
List<TestInfo> tstinfoCombo = new List<TestInfo>();
tstinfoCombo = tstinfoIn.Select(c => new TestInfo()
{
TestId = c.TestId,
TestShortDescription = c.TestShortDescription,
TestLongDescription = c.TestLongDescription,
TestTypeId = c.TestTypeId,
TestTypeInfos = GetTestTypeInfo(tsttypinfoIn, c.TestTypeId)
}).ToList();
tstinfoCombo = tstinfoCombo
.Where(x => x.TestTypeId == TestTypeId)
.Select(x => new TestInfo()
{
TestId = x.TestId,
TestShortDescription = x.TestShortDescription,
TestLongDescription = x.TestLongDescription,
TestTypeId = x.TestTypeId,
TestTypeInfos = x.TestTypeInfos
}).ToList();
return tstinfoCombo;
}
catch (Exception ex)
{
return null;
}
}
#endregion
namespace TestITWebService.Model
{
public partial class TestInfo
{
public int TestId { get; set; }
public string TestShortDescription { get; set; }
public string TestLongDescription { get; set; }
public int TestTypeId { get; set; }
public ICollection<TestTypeInfo> TestTypeInfos { get; set; }
}
}
#region "TestInfo"
modelBuilder.Entity<TestInfo>(entity =>
{
entity.HasKey(e => e.TestId);
entity.HasIndex(e => e.TestShortDescription)
.HasName("IX_TestInfo")
.IsUnique();
entity.Property(e => e.TestId).HasColumnName("TestID");
entity.Property(e => e.TestLongDescription)
.HasMaxLength(150)
.IsUnicode(false);
entity.Property(e => e.TestShortDescription)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.TestTypeId).HasColumnName("TestTypeID");
});
#endregion
#区域“TestIT/Testinfo/gettestinfo-bytesttypeid/1”
[HttpGet(“{TestTypeId}”,Name=“GetTestInfoByTestTypeID”)]
公共IEnumerable GetTestInfoByTestTypeID([FromRoute]int TestTypeId)
{
尝试
{
List tstinfoIn=_context.TestInfo.ToList();
List tsttypinfo=_context.TestTypeInfo.ToList();
List tstinfoCombo=新列表();
tstinfoCombo=tstinfoIn.Select(c=>newtestinfo()
{
TestId=c.TestId,
TestShortDescription=c.TestShortDescription,
TestLongDescription=c.TestLongDescription,
TestTypeId=c.TestTypeId,
TestTypeInfos=GetTestTypeInfo(TstTypeInfo,c.TestTypeId)
}).ToList();
tstinfoCombo=tstinfoCombo
.Where(x=>x.TestTypeId==TestTypeId)
.Select(x=>newtestinfo()
{
TestId=x.TestId,
TestShortDescription=x.TestShortDescription,
TestLongDescription=x.TestLongDescription,
TestTypeId=x.TestTypeId,
TestTypeInfos=x.TestTypeInfos
}).ToList();
返回tstinfoCombo;
}
捕获(例外情况除外)
{
返回null;
}
}
#端区
TestInfo模型:
#region "TestIT/Testinfo/GetTestInfoByTestTypeID/1"
[HttpGet("{TestTypeId}", Name = "GetTestInfoByTestTypeID")]
public IEnumerable<TestInfo> GetTestInfoByTestTypeID([FromRoute] int TestTypeId)
{
try
{
List<TestInfo> tstinfoIn = _context.TestInfo.ToList();
List<TestTypeInfo> tsttypinfoIn = _context.TestTypeInfo.ToList();
List<TestInfo> tstinfoCombo = new List<TestInfo>();
tstinfoCombo = tstinfoIn.Select(c => new TestInfo()
{
TestId = c.TestId,
TestShortDescription = c.TestShortDescription,
TestLongDescription = c.TestLongDescription,
TestTypeId = c.TestTypeId,
TestTypeInfos = GetTestTypeInfo(tsttypinfoIn, c.TestTypeId)
}).ToList();
tstinfoCombo = tstinfoCombo
.Where(x => x.TestTypeId == TestTypeId)
.Select(x => new TestInfo()
{
TestId = x.TestId,
TestShortDescription = x.TestShortDescription,
TestLongDescription = x.TestLongDescription,
TestTypeId = x.TestTypeId,
TestTypeInfos = x.TestTypeInfos
}).ToList();
return tstinfoCombo;
}
catch (Exception ex)
{
return null;
}
}
#endregion
namespace TestITWebService.Model
{
public partial class TestInfo
{
public int TestId { get; set; }
public string TestShortDescription { get; set; }
public string TestLongDescription { get; set; }
public int TestTypeId { get; set; }
public ICollection<TestTypeInfo> TestTypeInfos { get; set; }
}
}
#region "TestInfo"
modelBuilder.Entity<TestInfo>(entity =>
{
entity.HasKey(e => e.TestId);
entity.HasIndex(e => e.TestShortDescription)
.HasName("IX_TestInfo")
.IsUnique();
entity.Property(e => e.TestId).HasColumnName("TestID");
entity.Property(e => e.TestLongDescription)
.HasMaxLength(150)
.IsUnicode(false);
entity.Property(e => e.TestShortDescription)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.TestTypeId).HasColumnName("TestTypeID");
});
#endregion
namespace testtwebservice.Model
{
公共部分类TestInfo
{
公共int TestId{get;set;}
公共字符串TestShortDescription{get;set;}
公共字符串TestLongDescription{get;set;}
public int TestTypeId{get;set;}
公共ICollection TestTypeInfos{get;set;}
}
}
DBContext:
#region "TestIT/Testinfo/GetTestInfoByTestTypeID/1"
[HttpGet("{TestTypeId}", Name = "GetTestInfoByTestTypeID")]
public IEnumerable<TestInfo> GetTestInfoByTestTypeID([FromRoute] int TestTypeId)
{
try
{
List<TestInfo> tstinfoIn = _context.TestInfo.ToList();
List<TestTypeInfo> tsttypinfoIn = _context.TestTypeInfo.ToList();
List<TestInfo> tstinfoCombo = new List<TestInfo>();
tstinfoCombo = tstinfoIn.Select(c => new TestInfo()
{
TestId = c.TestId,
TestShortDescription = c.TestShortDescription,
TestLongDescription = c.TestLongDescription,
TestTypeId = c.TestTypeId,
TestTypeInfos = GetTestTypeInfo(tsttypinfoIn, c.TestTypeId)
}).ToList();
tstinfoCombo = tstinfoCombo
.Where(x => x.TestTypeId == TestTypeId)
.Select(x => new TestInfo()
{
TestId = x.TestId,
TestShortDescription = x.TestShortDescription,
TestLongDescription = x.TestLongDescription,
TestTypeId = x.TestTypeId,
TestTypeInfos = x.TestTypeInfos
}).ToList();
return tstinfoCombo;
}
catch (Exception ex)
{
return null;
}
}
#endregion
namespace TestITWebService.Model
{
public partial class TestInfo
{
public int TestId { get; set; }
public string TestShortDescription { get; set; }
public string TestLongDescription { get; set; }
public int TestTypeId { get; set; }
public ICollection<TestTypeInfo> TestTypeInfos { get; set; }
}
}
#region "TestInfo"
modelBuilder.Entity<TestInfo>(entity =>
{
entity.HasKey(e => e.TestId);
entity.HasIndex(e => e.TestShortDescription)
.HasName("IX_TestInfo")
.IsUnique();
entity.Property(e => e.TestId).HasColumnName("TestID");
entity.Property(e => e.TestLongDescription)
.HasMaxLength(150)
.IsUnicode(false);
entity.Property(e => e.TestShortDescription)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.TestTypeId).HasColumnName("TestTypeID");
});
#endregion
#区域“TestInfo”
modelBuilder.Entity(Entity=>
{
entity.HasKey(e=>e.TestId);
entity.HasIndex(e=>e.TestShortDescription)
.HasName(“IX_TestInfo”)
.IsUnique();
entity.Property(e=>e.TestId).HasColumnName(“TestId”);
属性(e=>e.TestLongDescription)
.HasMaxLength(150)
.IsUnicode(假);
属性(e=>e.TestShortDescription)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(假);
Property(e=>e.TestTypeId).HasColumnName(“TestTypeId”);
});
#端区
请将代码作为文本编辑您的问题。请不要给我们代码的截图。请参阅:删除图像并替换为代码。更新代码以仅显示重要的部件。。。。如果只是关于GetTestInfoByTestTypeID,那么当前代码很混乱,请更新以使其可读。有人可以分享关于如何让控制器只返回特定测试类型id的测试信息的建设性输入吗?我必须作为参数通过测试类型吗?任何例子都将不胜感激。