im currently trying to create a Controller in ASP.net core mvc with an optional parameter "id". Im fairly new to asp.net, I've tried to check other posts but nothing solved my problem.
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);
}
}
I want to open the site like this "localhost:6002/Test" or "localhost:6002/Test/123750349" e.g, the parameter can be an int as well, i've tried both(string and int) but it doesnt work.
Either the site returns 404 (for both cases, with and without an parameter) or the parameter gets ignored and is always null.
I've tried to add [Route("{id?}")] on the Index but it did not change anything.
Greetings