【发布时间】:2020-12-04 08:46:10
【问题描述】:
我目前正在尝试使用可选参数“id”在 ASP.net core mvc 中创建一个控制器。 我对 asp.net 相当陌生,我尝试查看其他帖子,但没有解决我的问题。
public class TestController : Controller
{
private readonly ITestRepository _TestRepository;
public TestController(ITestRepository TestRepository)
{
_TestRepository = TestRepository;
}
public async Task<IActionResult> Index(string id)
{
if (string.IsNullOrEmpty(id))
{
return View("Search");
}
var lieferscheinInfo = await _TestRepository.GetTestdata(Convert.ToInt64(id));
if (lieferscheinInfo == null)
{
// return err view
throw new Exception("error todo");
}
return View(lieferscheinInfo);
}
}
我想像“localhost:6002/Test”或“localhost:6002/Test/123750349”这样打开站点,例如,参数也可以是 int,我已经尝试过(字符串和 int)但是它不起作用。
要么站点返回 404(对于这两种情况,有和没有参数),要么参数被忽略并且始终为空。
我尝试在索引上添加[Route("{id?}")],但它没有改变任何东西。
问候
【问题讨论】:
-
Test 是控制器名称。基本上我想用可选参数调用我的控制器测试的 Index 方法
标签: c# asp.net asp.net-mvc asp.net-core