使用T4模板生成代码

时间:2013-09-27 15:18:24

标签: c# .net code-generation t4

我正在使用DataTables和jquery AJAX和Entity Framework。我从webmethod返回EntityFramework对象,将其序列化为json。我有很多表需要生成CRUD页面,因此在后端页面中会有CRUD webmethods。我之前用动态数据做过。有没有办法使用T4模板生成这些页面?我目前没有确切的代码,但最终结果将是这样的

http://editor.datatables.net/release/DataTables/extras/Editor/examples/envelope_inline.html

以下是.aspx.cs中的一些示例代码,它返回json

    [WebMethod]
    public string GetCustomers(int page)
    {
       return db.Customers.Skip(page*100).Take(100);
    }

   [WebMethod]
    public string DeleteCustomer(int id)
    {
        // ...
    }

在.aspx页面

$(document).ready(function() {
    var oTable = $('#example').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "scripts/server_processing.aspx"            
        }
    } );
} );

1 个答案:

答案 0 :(得分:2)

当然你可以这样做:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension="aspx.cs" #>
<# var crudObject= "Customer";
var plural = crudObject+"s"; #>

[WebMethod]
public string Get<#=plural#>(int page)
{
   return db.<#=plural#>.Skip(page*100).Take(100);
}

[WebMethod]
public string Delete<#=crudObject#>(int id)
{
    // ...
}