繁体   English   中英

如何使用asp.net mvc在视图中为控制器中的每个动作创建动作链接?

[英]How do I create an action link for each action in my controller within a view using asp.net mvc?

所以,我在控制器中有20多个Action ,如下所示:

public  ActionResult DoThis()
        {
            Image img = Image.FromFile("DoThis.png");
            //some other stuff to be done.
            return View();
        }
public  ActionResult DoThat()
        {
            Image img = Image.FromFile("DoThat.png");
            //some other stuff to be done.
            return View();
        }

我有一个视图,我想为放置在控制器中的每个操作显示ActionLink ,并为该操作分配图片,可以这样说:

@foreach
{
   <div class="col-sm-4">
    <div class="product-image-wrapper">
        <div class="single-products">
            <div class="productinfo text-center">
                <img src="the image assigned to the controller actions" alt="" />
                <h3 style="font-family:'Myriad عربي'">Action Name ؟</h3>
                <a href="@URL.Action("The acion link from controller")" class="btn btn-default add-to-cart">Click Here</a>
            </div>
        </div>
    </div>
</div>
}

那可能吗,或者我要求太多?

这是获取控制器上所有动作的ActionDescriptor[]列表的一种方法:

@{
    var descriptor = new ReflectedControllerDescriptor(this.ViewContext.Controller.GetType());
    var actions = descriptor.GetCanonicalActions();
}

@foreach (var action in actions)
{
    <div>
        @Html.ActionLink("click me", action.ActionName)
    </div>
}

当然,现在,这将获得当前控制器上所有动作的列表。 另一方面,如果您只想获取某些特定的动作,则可以用一些marker属性装饰它们,然后将检索到的动作过滤为仅具有该属性的动作:

public class MyActionAttribute: Attribute
{
}

...

public class HomeController: Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [MyAction]
    public ActionResult DoThis()
    {
        Image img = Image.FromFile("DoThis.png");
        //some other stuff to be done.
        return View();
    }

    [MyAction]
    public ActionResult DoThat()
    {
        Image img = Image.FromFile("DoThat.png");
        //some other stuff to be done.
        return View();
    }
}

接着:

@{
    var descriptor = new ReflectedControllerDescriptor(this.ViewContext.Controller.GetType());
    var actions = descriptor.GetCanonicalActions().Where(desc => desc.GetCustomAttributes(typeof(MyActionAttribute), true).Any());
}

好吧,您当然可以通过反射来获取特定类的方法:

public List<string> GetActionNames()
{
    return typeof(HomeController)
        .GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
        .Where(method => method.ReturnType.IsSubclassOf(typeof(ActionResult)) || method.ReturnType == typeof(ActionResult))
        .Select(method => method.Name)
        .ToList();
}

这将返回控制器上定义的所有动作的名称,其中任何返回与ActionResult兼容的类型的东西都被视为动作方法,而没有别的东西。 但是,您应该考虑以下几点:

  • 您将如何决定要绑定到哪个参数的内容,
  • 您将如何处理重载的方法,
  • 您应该根据任何安全需求和其他限制来完善此逻辑,
  • 如果任何方法都是非HttpGet方法,则应该进一步过滤它们,可以通过HttpPost属性的方法来过滤方法(如果要处理ApiController ,这会变得更加复杂)考虑基于名称前缀的方法约定)。

一般而言,第一个要点是最令人关注的问题,您只需手动输入链接,而不是尝试找出一种可靠且通用的解决方法,就会更好。 因此,我只建议您在可以确定不必担心上述问题的情况下使用这种逻辑。

暂无
暂无

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

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