简体   繁体   English

如何使C#和Javascript代码保持同步?

[英]How to keep C# and Javascript Code in sync?

I should think this problem could come to anyone, I'm interested in ways of making the best use of technology to keep my code-base DRY and in sync. 我应该认为这个问题可能会发生在任何人身上,我对充分利用技术来保持代码库DRY并保持同步的方式感兴趣。

I'm developing an ASP.NET MVC5 web application. 我正在开发一个ASP.NET MVC5 Web应用程序。 I have several variables I need to be communicating to the client side. 我需要与客户端通信的几个变量。 Say "PageType", which I need to be tracked on the client side using Google Analytics. 说“ PageType”,我需要使用Google Analytics(分析)在客户端进行跟踪。 The intent may be multifarious, this is a simple application with the same purpose, for illustration. 目的可能是多种多样的,出于说明目的,这是一个具有相同目的的简单应用程序。

I have a C# class which allows me to specify the various PageTypes and also switch between them for switch-casing, say: 我有一个C#类,它允许我指定各种PageType,也可以在它们之间进行切换,例如:

public static class PageTypes {
    public const String Home = "Home";
    public const String AboutUs = "AboutUs";
    public const String ContactUs = "ContactUs";
}

This is useful when we check dynamic values, like: 当我们检查动态值时,这很有用:

String pageType = ViewBag.PageType;
if(!String.IsNullOrWhiteSpace(pageType))
{
    switch(pageType)
    {
        case PageTypes.Home:
            // do something here
            break;
        case PageTypes.AboutUs:
            // do something else here
            break;
        ...

... or even when doing comparisons. ...甚至进行比较时。 I hope you get the point. 我希望你明白这一点。 So, in a Razor View, I can output var pageType = '@ViewBag.PageType'; 因此,在Razor视图中,我可以输出var pageType = '@ViewBag.PageType'; in a page in an inline script, and it will be present as var pageType = 'Home'; var pageType = 'Home';联脚本的页面中,它将以var pageType = 'Home'; or some such as the case, in the inline javascript of the page. 或某些情况(例如网页的内嵌javascript)。 Now, just as I did with the C# code, I want to be able to switch-case the page type inside my javascript. 现在,就像使用C#代码一样,我希望能够在JavaScript中切换大小写形式。

// This part is dynamic so will be inline script of the razor view
var pageType = 'Home';

// This part is logic so can reside in an external script

// This part is just illustration, I will worry about naming and accessibility later
// This will be in a separate script and will be included, and will be referenced, thereby providing Intellisense within Visual Studio
var PageTypes = {
    Home: 'Home',
    AboutUs: 'AboutUs'
};

// I should be able to do this, in an external script
switch(pageType){
    case PageTypes.Home:
        alert('Home!');
        break;
    case PageTypes.AboutUs:
        alert('Not Home');
        break;
    default:
        alert('Nothing');
        break;
}

It can be seen that if the static class PageTypes of C# is in sync with the var PageTypes in the JavaScript, it would be a boon to our developers, with the Intellisense and predefined values, so that they may avoid typos and incompatibilities of values. 可以看出,如果C#的static class PageTypes与JavaScript中的var PageTypes同步,则对我们的开发人员来说,它具有Intellisense和预定义的值将是一个福音,因此它们可以避免输入错误和值不兼容。

What's the best way to achieve this, while gaining Intellisense in both C# and JavaScript? 在C#和JavaScript中获得Intellisense的同时,实现此目标的最佳方法是什么?

I would prefer changes in C# affecting JS rather than the reverse. 我希望C#中的更改会影响JS,而不是相反。 The switch statements need not be converted, only the constant values like what I have mentioned. switch语句不需要转换,只需转换常量值就可以了。

I think a script in the build process could be useful but is there any tool/plugin/alternate way to accomplish what I have mentioned? 我认为构建过程中的脚本可能有用,但是是否有任何工具/插件/替代方法可以完成我提到的内容?

If I have understand what the problem is, what you can do is: 如果我了解问题所在,则可以执行以下操作:

  • set the pagetype in some variable inside your html markup, using the template engine (sorry, I don't work with .Net, don't know how to write it). 使用模板引擎在html标记内的某个变量中设置pagetype(对不起,我不使用.Net,不知道如何编写)。 In this example, I used the data-* in the body element 在此示例中,我在body元素中使用了data- *

< body data-type="%some template engine syntax that output the page type%"> <body data-type =“%some模板引擎语法,输出页面类型%”>

  • in your js, retrieve the data-type value (in the example, I used jquery) 在您的js中,检索数据类型值(在示例中,我使用了jquery)

    var type = $("body").data("type"); var type = $(“ body”)。data(“ type”);

  • define some functions, each one for your pagetype, inside an key/object variable / object 在键/对象变量/对象内定义一些函数,每个函数用于您的页面类型

    var PageTypeFunctions = { home: function() { var PageTypeFunctions = {home:function(){

     }, aboutus: function() { }, xpto: function() { } 

    } }

  • invoke the function from the 从中调用函数

    PageTypeFunctionstype; PageTypeFunctionstype;

With this approach, you won't have to deal with the PageTypes variable. 使用这种方法,您将不必处理PageTypes变量。 You will only need one function for each pagetype. 每个页面类型只需要一个功能。 Of course you can do some check to validate if PageTypeFunctions[type] exists. 当然,您可以做一些检查来验证PageTypeFunctions [type]是否存在。

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

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