asp-controller和asp-action属性不起作用

时间:2016-08-05 20:32:58

标签: asp.net-core asp.net-core-mvc

有谁知道我缺少什么,为什么那些asp-controller和asp-action标签对我不起作用。我正在ASP.NET MVC Core中实现一个项目。

这不会触发:

<a asp-controller="App" asp-action="Trips" class="btn btn-lg btn-success">Go to Trips</a>

Razor工作正常:

@Html.ActionLink("Go to Trips", "Trips", "App", new object { }, new { @class = "btn btn-lg btn-success" })

我是否需要配置一些服务才能正常工作。而且,哪种方式更受欢迎? Razor很受MVC欢迎,那些asp标签是一种新的,更好的方式吗?

5 个答案:

答案 0 :(得分:64)

经过一番挖掘后,我发现asp-controller和asp-action属性被称为锚标记助手,并且是

的一部分。
  

Microsoft.AspNetCore.Mvc.TagHelpers名称空间

显然它是使用Razor的替代方案。我能够通过创建'_ViewImports.cshtml'并将以下内容添加到文件中来解决此问题:

    *&---------------------------------------------------------------------*
*&  Include           ZAUSP_SLT_TRANSFORM
*&---------------------------------------------------------------------*


STATICS lv_syn_name TYPE tabname.

DATA con_name TYPE dbcon_name.

DATA lv_timestamp TYPE timestampl.

FIELD-SYMBOLS <lv_operation> TYPE any.


CONCATENATE _mt_id ':R:R' INTO con_name.

ASSIGN COMPONENT 'IUUC_OPERAT_FLAG' OF STRUCTURE <wa_s_AUSP> TO <lv_operation>.

IF sy-subrc = 0 AND
   <lv_operation> = 'D'.

  IF lv_syn_name IS INITIAL.
    CALL METHOD cl_iuuc_tab_ident_access=>get_ident
      EXPORTING
        iv_mt_id       = _mt_id
        iv_tabname     = _cobj_alias
        iv_system_type = cl_iuuc_tab_ident_access=>co_system_receiver
        iv_ident_type  = cl_iuuc_tab_ident_access=>co_ident_synonym
      IMPORTING
        ev_full_name   = lv_syn_name.
    IF lv_syn_name IS INITIAL.
      allog_msg 'E' 'DMC_RT_MSG' '000'
      'Get Synonym Error' space space space 'IL '.
      RAISE stopped_by_rule.
    ENDIF.
  ENDIF.

  GET TIME STAMP FIELD lv_timestamp.

  UPDATE (lv_syn_name) CLIENT SPECIFIED CONNECTION (con_name)
    SET ZDELETE_FLAG = 'X'
        ZUPD_DATETIME = lv_timestamp
    WHERE mandt  = <wa_s_AUSP>-mandt
      AND objek = <wa_s_AUSP>-objek
      AND atinn = <wa_s_AUSP>-atinn
      AND atzhl = <wa_s_AUSP>-atzhl
      AND mafid = <wa_s_AUSP>-mafid
      AND klart = <wa_s_AUSP>-klart
      AND adzhl = <wa_s_AUSP>-adzhl.


  IF sy-subrc <> 0.
    allog_msg 'E' 'DMC_RT_MSG' '000'
    'Update Error' space space space 'IL '.
    RAISE stopped_by_rule.
  ENDIF.

  skip_record.
ENDIF.

"Code for timestamp outside of delete.

 IF lv_syn_name IS INITIAL.
    CALL METHOD cl_iuuc_tab_ident_access=>get_ident
      EXPORTING
        iv_mt_id       = _mt_id
        iv_tabname     = _cobj_alias
        iv_system_type = cl_iuuc_tab_ident_access=>co_system_receiver
        iv_ident_type  = cl_iuuc_tab_ident_access=>co_ident_synonym
      IMPORTING
        ev_full_name   = lv_syn_name.
    IF lv_syn_name IS INITIAL.
      allog_msg 'E' 'DMC_RT_MSG' '000'
      'Get Synonym Error' space space space 'IL '.
      RAISE stopped_by_rule.
    ENDIF.
  ENDIF.

  GET TIME STAMP FIELD lv_timestamp.

  UPDATE (lv_syn_name) CLIENT SPECIFIED CONNECTION (con_name)
    SET   ZUPD_DATETIME = lv_timestamp          "slt_update is the name in your slt strucure!
    WHERE MANDT  = <wa_s_AUSP>-MANDT             "key_1 = pk of your table
    AND   OBJEK  = <wa_s_AUSP>-OBJEK
    AND   ATINN  = <wa_s_AUSP>-ATINN
    AND   ATZHL  = <wa_s_AUSP>-ATZHL
    AND   MAFID  = <wa_s_AUSP>-MAFID
    AND   KLART  = <wa_s_AUSP>-KLART
    AND   ADZHL  = <wa_s_AUSP>-ADZHL.

   IF sy-subrc <> 0.
    allog_msg 'E' 'DMC_RT_MSG' '000'
    'Update Error' space space space 'IL '.
    RAISE stopped_by_rule.
  ENDIF.

完成此操作后,会识别锚标记助手并按钮开始按预期工作。

答案 1 :(得分:6)

在使用Areas时,我不得不将_ViewImport复制到新的Area \ MyArea \ Views

答案 2 :(得分:1)

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers添加到cshtml文件顶部是可行的。

答案 3 :(得分:0)

s = df['start_time'] - df.shift()['start_time']  < .05
df = df[~s]
df
Out[1]: 
   peak_time  start_time   end_time
1  30.691333   30.670667  30.710333
2  36.918000   36.908667  36.932333
4  37.210333   37.197333  37.306333

Razor页面具有从“ PageModel”继承的modelcshtml.cs文件,因此在我而言,这些文件中的操作方法未被识别...我用asp-page替换了asp-controller,用asp-action替换了asp-action页面处理程序。加上在startup.cs中为默认路由添加了一行。

@{    
  IDictionary<string, string> routeData = new Dictionary<string, string>();
  routeData.Add("Id", point.Id.ToString());
  routeData.Add("Name", point.Name);
  routeData.Add("Address", point.Address);
}
<a asp-page="./AddCollectionPoint" asp-page-handler="Edit" asp-all-route-data="@routeData">

成功了。

答案 4 :(得分:0)

我花了一些时间才弄明白——但如果你像我一样还在苦苦挣扎 Cannot resolve symbol 'addTagHelper' 尝试 accepted answer 后,您可能正在运行 ASP.NET MVC 项目而不是 ASP.NET Core 项目,因此无法使用 taghelper。相反,您必须使用 htmlhelper

TLDR;

如果 this answer 不适合您, 请使用htmlhelper (*)

@using (Html.BeginForm("Trips", "App")) 
{
 ...
}

.. 而不是 taghelper (**)

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers


<form asp-controller="App" asp-action="Trips">
   ...
</form>

(*) 适用于

<块引用>

ASP.NET MVC 5.2

(**) 适用于

<块引用>

ASP.NET 核心 2.1、1.0、1.1、2.0、2.2、3.0、3.1、5.0