服务器端和客户端方法

时间:2021-07-23 15:50:59

I'm using ASP.NET with C# 2.0. I have created some objects for a database and each of these objects has properties which can be called natively or are called in a similar manner and create a RESTful JSON API from them.

我用ASP。净与c# 2.0。我为数据库创建了一些对象,每个对象都具有属性,这些属性可以本机调用或以类似的方式调用,并从中创建一个RESTful JSON API。

I have a lot of tab-like things I like to call 'modules' on this site - the function of a module is to convert data to HTML to be displayed on the page. Idealy this needs to be done in both server side C# code for the first tab to load, then use Ajax to load the others when the tabs are clicked, however for old browsers and search engines, the tab is still a link that will load the same HTML code server side.

我有很多类似于表格的东西,我喜欢在这个网站上称之为“模块”——模块的功能是将数据转换成HTML显示在页面上。进而这需要在服务器端完成c#代码加载第一个选项卡,然后使用Ajax加载其他选项卡单击时,然而对于旧的浏览器和搜索引擎,标签仍然是一个链接,将负载相同的HTML代码服务器端。

Currently I've writting the JavaScript code completely separately from the C# code that converts each module to HTML, but the method is virtually the same, just a different language. Similar to this example.

目前,我已经将JavaScript代码与将每个模块转换为HTML的c#代码完全分开了,但是这个方法实际上是一样的,只是一种不同的语言。类似的例子。

C# code

c#代码

public override string GetHtml()
{
    IJsonObjectCollection<Person> response = ((Village)page).People;
    string html = "<div id=\"test\">";
    foreach (Person person in response)
    {
        html += "<div class=\"person\">";
        html += person.Name;
        if(canEdit) html += "*";
        html += "</div>";
    }
    return html + "</div>";
}

JavaScript code

JavaScript代码

function getHtml() {
    JsonRequest('/json/villages/1/people', function(response) {
        var html = '<div id="test">';
        for (int i = 0; i < response.length; i++)
        {
            var person = response[i];
            html += '<div class="person">';
            html += person.name;
            if(canEdit) html += '*';
            html += '</div>';
        }
        return html + '</div>';
    });
}

You can probably see where I'm going with this question. What would be the most efficient way of doing this? I was thinking of a few different alternatives -

你可能会明白我的问题。最有效的方法是什么?我想到了几个不同的选择。

1. Each ModuleToHtmlMethod could be a class that defines the method of turning this data object into HTML. I attempted this, but I stopped because I was getting too complicated.

1。每个ModuleToHtmlMethod可以是一个类,它定义将这个数据对象转换为HTML的方法。我尝试了一下,但是我停下来了,因为我太复杂了。

2. Write my own scripting language that can be interpreted as C# but also 'compiled' into JavaScript code.

2。编写我自己的脚本语言,它可以解释为c#,也可以“编译”成JavaScript代码。

3. Just write the lot in C# and use Ajax to simply request the HTML content from C#

3所示。只需要用c#编写大量内容,并使用Ajax从c#请求HTML内容

4. Keep the code separated and write every method twice.

4所示。将代码分隔开,并编写每个方法两次。

I'd like to eventually allow other developers to write these 'modules', so maybe option 2 is the best option?

我希望最终允许其他开发人员编写这些“模块”,所以也许选项2是最好的选择?

2 个解决方案

#1


1  

I would discard option 4 as it will make maintenance more difficult and you may end up out of synch between the HTML generated via the Javascript and the one from the C# code. I would also discard the option 2 as that may make the code more difficult for other developers and also probably unnecessary.

我将放弃选项4,因为这会使维护更加困难,您可能会在通过Javascript生成的HTML与来自c#代码的HTML之间失去同步。我还将放弃选项2,因为这可能使代码对其他开发人员来说更加困难,而且可能也没有必要。

I would definitely generate the HTML in one place and probably expose RESTful HTML API that uses the C# existing function to return the HTML snippets. So from your Javascript you would call:

我肯定会在一个地方生成HTML,并且可能会公开使用c#现有函数返回HTML片段的RESTful HTML API。你可以从Javascript调用:

function getHtml() {
    MyHtmlRequest('/html/villages/1/people', function(response) {
        var html = response.Text;
        return html;
    });
}

#2


1  

A couple of suggestions.

两个建议。

  • Have a generic GetHtml method that reflects the html. This can be hard as UI is not something that maps easily and uniformly to data fields.
  • 具有反映html的通用GetHtml方法。这可能很困难,因为UI不是一种可以轻松、一致地映射到数据字段的东西。
  • Have a meta description of your 'modules', use this to create your generic GetHtml methods
  • 有一个元描述你的“模块”,用它来创建你的通用GetHtml方法
  • Finally try this: It will allow you just to create JavaScript methods, you can then call them from C#
  • 最后尝试一下:它只允许您创建JavaScript方法,然后您可以从c#调用它们

I would go for the second, meta description option as this is what I do for my data layers. I basically have a file defining my domain model. I use this to generate all my data acccess pocos, nhibernate config files, etc. I then have a meta data file which adds information to these objects, like UI rendering information and field validation info.

我会选择第二个,meta description选项,因为这是我对数据层所做的。我基本上有一个定义域模型的文件。我使用它来生成所有我的数据处理程序pocos、nhibernate配置文件等等。然后我有一个元数据文件,它向这些对象添加信息,比如UI呈现信息和字段验证信息。

Tnx

Tnx

Guido

圭多

#1


1  

I would discard option 4 as it will make maintenance more difficult and you may end up out of synch between the HTML generated via the Javascript and the one from the C# code. I would also discard the option 2 as that may make the code more difficult for other developers and also probably unnecessary.

我将放弃选项4,因为这会使维护更加困难,您可能会在通过Javascript生成的HTML与来自c#代码的HTML之间失去同步。我还将放弃选项2,因为这可能使代码对其他开发人员来说更加困难,而且可能也没有必要。

I would definitely generate the HTML in one place and probably expose RESTful HTML API that uses the C# existing function to return the HTML snippets. So from your Javascript you would call:

我肯定会在一个地方生成HTML,并且可能会公开使用c#现有函数返回HTML片段的RESTful HTML API。你可以从Javascript调用:

function getHtml() {
    MyHtmlRequest('/html/villages/1/people', function(response) {
        var html = response.Text;
        return html;
    });
}

#2


1  

A couple of suggestions.

两个建议。

  • Have a generic GetHtml method that reflects the html. This can be hard as UI is not something that maps easily and uniformly to data fields.
  • 具有反映html的通用GetHtml方法。这可能很困难,因为UI不是一种可以轻松、一致地映射到数据字段的东西。
  • Have a meta description of your 'modules', use this to create your generic GetHtml methods
  • 有一个元描述你的“模块”,用它来创建你的通用GetHtml方法
  • Finally try this: It will allow you just to create JavaScript methods, you can then call them from C#
  • 最后尝试一下:它只允许您创建JavaScript方法,然后您可以从c#调用它们

I would go for the second, meta description option as this is what I do for my data layers. I basically have a file defining my domain model. I use this to generate all my data acccess pocos, nhibernate config files, etc. I then have a meta data file which adds information to these objects, like UI rendering information and field validation info.

我会选择第二个,meta description选项,因为这是我对数据层所做的。我基本上有一个定义域模型的文件。我使用它来生成所有我的数据处理程序pocos、nhibernate配置文件等等。然后我有一个元数据文件,它向这些对象添加信息,比如UI呈现信息和字段验证信息。

Tnx

Tnx

Guido

圭多