简体   繁体   English

Asp.net 核心 mvc 可选参数索引

[英]Asp.net core mvc optional parameter for Index

im currently trying to create a Controller in ASP.net core mvc with an optional parameter "id".我目前正在尝试使用可选参数“id”在 ASP.net 核心 mvc 中创建 Controller。 Im fairly new to asp.net, I've tried to check other posts but nothing solved my problem.我对 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);

    }
}

I want to open the site like this "localhost:6002/Test" or "localhost:6002/Test/123750349" eg, the parameter can be an int as well, i've tried both(string and int) but it doesnt work.我想像“localhost:6002/Test”或“localhost:6002/Test/123750349”这样打开站点,例如,参数也可以是 int,我已经尝试过(字符串和 int)但它不起作用. Either the site returns 404 (for both cases, with and without an parameter) or the parameter gets ignored and is always null.站点要么返回 404(对于这两种情况,有参数和没有参数),要么参数被忽略并且始终为 null。 I've tried to add [Route("{id?}")] on the Index but it did not change anything.我试图在索引上添加[Route("{id?}")]但它没有改变任何东西。

Greetings问候

In your project's Startup class make sure you are using the right MapControllerRoute在您项目的启动 class 确保您使用正确的 MapControllerRoute

 app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Test}/{action=Index}/{id?}");

        });

your code should work just fine.您的代码应该可以正常工作。 string parameters accepts null by default so you dont need to specify can you check how the routing is set up in your startup.cs file.字符串参数默认接受 null 所以你不需要指定你可以在你的 startup.cs 文件中检查路由是如何设置的。

You can add routing through attribute for example the following:您可以通过属性添加路由,例如以下内容:

[Route("Test")]
[Route("Test/Index")]
[Route("Test/Index/{id?}")]

Will allow you to access the Action using:将允许您使用以下方式访问操作:

/test/?id=sasasa
test/Index/?id=sasasa 
test/Index

check Routing Documentation at Microsoft site: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1检查 Microsoft 站点上的路由文档: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1

Update the MapControllerRoute in the Startup.cs更新 Startup.cs 中的 MapControllerRoute

app.UseEndpoints(endpoints => {
     endpoints.MapControllerRoute(
         name : "default",
         pattern: "{controller=Test}/{action=Index}/{id?}"
     );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM