简体   繁体   English

你如何在F#中使用连字符制作一个控制器?

[英]how do you make a controller in F# that uses hyphens?

I have a .net MVC website and my controllers are written in f#. 我有一个.net MVC网站,我的控制器是用f#编写的。 But I'm having trouble finding out how to use hyphens in my controllers. 但我无法找到如何在我的控制器中使用连字符。

I want something like: 我想要的东西:

domain.com/some-cool-page domain.com/some-cool-page

How is this done in F#? 这是如何在F#中完成的?

Here's an example of my contact controller: 这是我的联系人控制器的一个例子:

type ContactController() =
inherit Controller()
member x.Index () =
    let version = typeof<int * int>.Assembly.GetName().Version
    let msg = sprintf "This web page is running using F# version %s." (version.ToString(4))
    x.ViewData.["Message"] <- msg

    let uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri)
    let param1 = HttpUtility.ParseQueryString(uri.Query).Get("id") 
    x.ViewData.["idinfo"] <- param1
    x.View("~/Views/contact.cshtml")

Here's one way to do it with routing configuration: 以下是使用路由配置执行此操作的一种方法:

routes.MapRoute(
    "CoolPage",
    "some-cool-page/{action}/{id}",
    { controller = "Contact"; action = "Index"; id = UrlParameter.Optional }
) |> ignore

Add this before the "Default" route, and you can access the ContactsController and the appropriate view via /some-cool-page . 在“默认”路由之前添加它,您可以通过/some-cool-page访问ContactsController和相应的视图。

You'll typically find the routing configuration in your global.asax.fs file, depending on the template you're using. 您通常会在global.asax.fs文件中找到路由配置,具体取决于您使用的模板。

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

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