Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 停止在运行时执行的函数/句柄路由_Asp.net Core_F# - Fatal编程技术网

Asp.net core 停止在运行时执行的函数/句柄路由

Asp.net core 停止在运行时执行的函数/句柄路由,asp.net-core,f#,Asp.net Core,F#,在ASP.NET MVC中使用F#模板。是否定义了一个la app.UseRouting() .UseEndpoints(fun endpoints -> endpoints.MapGet("/", fun context -> context.Response.WriteAsync(PageController.loadHomePage())) |> ignor

在ASP.NET MVC中使用F#模板。是否定义了一个la

 app.UseRouting()
           .UseEndpoints(fun endpoints ->
                endpoints.MapGet("/", fun context ->
                    context.Response.WriteAsync(PageController.loadHomePage())) |> ignore

                endpoints.MapGet("/contact", fun context ->
                    context.Response.WriteAsync(PageController.loadContactPage())) |> ignore

                endpoints.MapGet("/about", fun context ->
                    context.Response.WriteAsync(PageController.loadAboutPage())) |> ignore
然后是控制器函数,所有这些函数都加载同一个页面,并简单地将其分为一个标题变量。addVariable函数simple使用调用loadTemplate时提供的字符串替换“[title]”的所有实例

module PageController

let loadHomePage() =
     Theme.addVariable "title" "Home Page"
     Theme.loadTemplate "index"


let loadContactPage() =
    Theme.loadTemplate "index"

let loadAboutPage() =
    Theme.addVariable "title" "About Us"
    Theme.loadTemplate "index"

但是,当我导航到未定义标题变量的
/contact
时,它的标题为“主页”,这意味着正在运行
loadHomePage()
,并设置标题变量

我是ASP.NET和F#的新手,所以假设它非常简单

编辑:

(*添加变量函数将值添加到字典*)
让mutable themeVars=newdictionary()
让addVariable name值=
themeVars.[姓名]
让varName=matchedVar.Groups.[1].ToString()
|>弦饰
如果mevars.ContainsKey varName,则
themeVars.[varName]
其他的
“[未定义的变量:“+varName+”]””
))
让LoadTemplateTemplateName=
如果templateExists templateName,则
模板名
|>getTemplateFilePath
|>File.ReadAllText
|>解析变量
其他的
缺少模板:“+templateName+”

我认为这里的问题是您有一个由所有三个页面共享的可变
themeVars
字典。当应用程序启动时,将加载主页,这将向字典中添加一个名为
“Title”
的变量。然后,当您访问Contact页面时,该变量仍然存在于字典中,不管它最后的值是什么。(可以是
“主页”
“关于我们”

为了避免这种情况,请为每个页面创建单独的不可变变量查找。大概是这样的:

let loadHomePage() =
    let themeVars = Map [ "Title", "Home Page" ]
    Theme.loadTemplate themeVars "index"

let loadContactPage() =
    let themeVars = Map.empty
    Theme.loadTemplate themeVars "index"

let loadAboutPage() =
    let themeVars = Map [ "Title", "About Us" ]
    Theme.loadTemplate themeVars "index"

请注意,我在这里使用了
映射
而不是
字典
,因为它们在F#中更容易处理,但这两种方法的思想都是一样的。

您能否共享
添加变量
加载模板
的实现,以便我们可以编译您的代码。Addvariables在空字典中设置一个键。所以我想要的是:当我访问
/contact
时,我应该得到“[Undefined Variable:title]”,而我得到的是
主页
,这意味着loadHomePage()在某个时候正在运行。这让我从解释语言转向编译语言有些困惑,所以这可能是一个更基本的概念。我对从一个页面泄漏到另一个页面甚至跨用户的值很谨慎。ASP是否跟踪会话并为用户使用相同的实例?如果同一用户在不同选项卡中打开同一页,在一个选项卡上导航到另一页,然后在另一个选项卡上刷新该页,该怎么办?(在本地测试,如果我在Firefox和Opera中打开应用程序,在两者中导航到contact,然后在Firefox中导航到About并在Opera中刷新实例,它也会将“About”设置为标题。所以他们使用的是完全相同的实例/会话?关于如何处理,我能做更多的阅读吗?一般来说,你希望避免使用(比如你的
themeVars
dictionary),这会导致你提到的问题。在函数式编程中,我们通过编写“pure”来实现没有副作用的函数。通过这种方式,您可以确定加载X页不会对Y页的行为产生不必要的影响。是的,对不起,我明白了,并感谢代码解决方案。我认为“问题”对我来说,现在是更大的图景,正确理解编译后的web应用程序是如何工作的。在我看来,允许对变量的更改在浏览器中持久化似乎很奇怪。问题更多的是,对变量的更改会持久化在服务器的内存中。发送到浏览器的页面只是服务器在t如果你可以避免在服务器上共享可变数据(通常称为“无状态”架构),你就不必担心诸如实例、会话、多个选项卡、多个浏览器等复杂问题。
let loadHomePage() =
    let themeVars = Map [ "Title", "Home Page" ]
    Theme.loadTemplate themeVars "index"

let loadContactPage() =
    let themeVars = Map.empty
    Theme.loadTemplate themeVars "index"

let loadAboutPage() =
    let themeVars = Map [ "Title", "About Us" ]
    Theme.loadTemplate themeVars "index"