将代码从 C# 转换为 VB.NET [英] translate code from C# to VB.NET

查看:41
本文介绍了将代码从 C# 转换为 VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把它从 C# 翻译成 VB.NET

I translated this from C# to VB.NET

C#:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        var config = new HttpConfiguration() { EnableTestClient = true };
        routes.Add(new ServiceRoute("api/contacts", new HttpServiceHostFactory() { Configuration = config }, typeof(ContactsApi)));

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

VB.NET:

    Public Shared Sub RegisterRoutes(routes As RouteCollection)
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

        Dim config = New HttpConfiguration() With { _
            Key .EnableTestClient = True _
        }
        routes.Add(New ServiceRoute("api/contacts", New HttpServiceHostFactory() With { _
            Key .Configuration = config _         <-----------Name of field or property being initialized in an object initializer must start with '.'. 
        }, GetType(ContactsApi)))

        ' Route name
        ' URL with parameters
        ' Parameter defaults
        routes.MapRoute("Default", "{controller}/{action}/{id}", New With { _
         Key .controller = "Home", _
         Key .action = "Index", _
         Key .id = UrlParameter.[Optional] _
        })
    End Sub     

但我收到一个错误(内联在 VB.NET 代码中):

But I get an error (placed inline in VB.NET code):

正确的翻译是什么?

推荐答案

移除 Key.

Public Shared Sub RegisterRoutes(routes As RouteCollection)
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

    Dim config = New HttpConfiguration() With { _
        .EnableTestClient = True _
    }
    routes.Add(New ServiceRoute("api/contacts", New HttpServiceHostFactory() With { _
        .Configuration = config _         <-----------Name of field or property being initialized in an object initializer must start with '.'. 
    }, GetType(ContactsApi)))

    ' Route name
    ' URL with parameters
    ' Parameter defaults
    routes.MapRoute("Default", "{controller}/{action}/{id}", New With { _
         .controller = "Home", _
         .action = "Index", _
         .id = UrlParameter.[Optional] _
    })
End Sub     

这篇关于将代码从 C# 转换为 VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆