C# 使用空/空字符串作为参数调用控制器操作

C# 使用空/空字符串作为参数调用控制器操作,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我的控制器中有一个动作,它采用2-3个参数,如下所示 public ActionResult MyAction(string str1, string str2, string str3) { // code here } 在视图中显示参数和结果时,将使用一些有效值调用此操作。现在,我想再次调用相同的操作,并将空/空字符串值作为参数传递。但当我这样称呼这个行动时: @Html.ActionLink("Click", "MyAction", &

我的控制器中有一个动作,它采用2-3个参数,如下所示

public ActionResult MyAction(string str1, string str2, string str3)
{
     // code here 
}
在视图中显示参数和结果时,将使用一些有效值调用此操作。现在,我想再次调用相同的操作,并将空/空字符串值作为参数传递。但当我这样称呼这个行动时:

@Html.ActionLink("Click", "MyAction", "MyController") 
路由模板如下所示:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{str1}/{str2}/{str3}",
        new {controller = "MyController", action = "MyAction", str1 = UrlParameter.Optional, str2 = UrlParameter.Optional, str3 = UrlParameter.Optional}
    );
}
我得到了与参数相同的值,如何使用空/空值作为参数调用操作?

试试这段代码

@Html.ActionLink("Click", "MyAction", "MyController", new { str1 = "", str2 = "" , str3 = "" }) 
您可以在此处使用C#可选参数

public ActionResult MyAction(string str1="", string str2="", string str3=null)
{
     // code here 
}

“与参数值相同”是什么意思
@Html.ActionLink(“单击”、“MyAction”、“MyController”)
应使用空值为parameters@RajdeepDebnath我的意思是,当第一次调用此操作时,我得到了与参数相同的值,当再次调用此操作时,我得到了相同的值。我已按照您的建议使用,但是我仍然得到了值:(我已经按照你的建议使用了,但是我仍然得到了值:(例如,你得到的值是多少?
str1
?与第一次调用action方法的值相同?你是否尝试清除缓存?或者使用其他浏览器。
@Html.ActionLink("link", "About", "Home")

public ActionResult MyAction(string str1="", string str2="", string str3=null)
{
    //values
     str1=="";
     str2=="";
     str3==null;
}
@Html.ActionLink("link", "About", "Home", new { str1="a", str2="b", str3="c" })

public ActionResult MyAction(string str1="", string str2="", string str3=null)
{
    //values
     str1=="a";
     str2=="b";
     str3=="c";
}