【问题标题】:Nancy redirect to route under the current moduleNancy 重定向到当前模块下的路由
【发布时间】:2017-01-27 23:17:53
【问题描述】:

我需要重定向到当前模块下的一个路径:

modulePath = "/test";  
Get["/there"] = ...  
Get["/here"] = routeParams => { return ????????????("/there"); } 

我希望“/test/here”重定向到“/test/there”

【问题讨论】:

  • return Response.AsRedirect("/test/there");
  • 是否可以在不指定模块路径的情况下做到这一点? ("/test")
  • 不,这不可能。
  • @Phill 你能把它作为一个答案,以便它可以被标记吗? (而不是作为评论)

标签: nancy


【解决方案1】:

您可以使用命名路由和 Nancy.Linker。见https://github.com/horsdal/Nancy.Linker

即从 NuGet 安装 Nancy.Linker,依赖 IResourceLinker 并将代码更改为:

public class YourModule
{
    public YourModule(IResourceLinker linker)
    {
        Get["theRoute", "/there"] = ...  
        Get["/here"] = Response.AsRedirect(linker.BuildAbsoluteUri(this.Context, "theRoute");
    }
{

【讨论】:

    【解决方案2】:

    您可以使用我的 BaseModule。它将让您将 Web 应用程序组织成松散耦合的单元,相关元素位于同一文件夹中:模块、视图、静态资源。这是通过将模块的命名空间(项目名称的缩写)注册为 1) modulePath、2) 视图的搜索位置和 3) 静态内容的允许位置来实现的。

    因此,您可以(返回)使用映射到项目文件夹结构的 URL。您可能一起处理和删除的项目,一起生活。构建到事物的链接被大大简化了。因此,对于您的重定向,您将拥有一个文件夹结构

    这里 |-模块.cs |-那里 |-模块.cs

    在您的“这里”模块中,您只需 return Response.AsRedirect("there/"); 为此,模块应该从以下 BaseModule 继承,并且不要更改模块的默认命名空间。它映射到文件夹。

    using Nancy;
    using Nancy.Conventions;
    using System;
    using System.Text.RegularExpressions;
    
    namespace HaciendaTestClient
    {
        // http://stackoverflow.com/questions/2287636/pass-current-object-type-into-base-constructor-call
        public abstract class BaseModule<T> : NancyModule, IConvention
        {
            // the goal is to provide the folder path from the app root as the module path, to enable relative links to static content.
            public BaseModule() : base(typeof(T).Namespace.Substring(typeof(T).Namespace.IndexOf('.')).Replace('.', '/'))
            {
            }
    
            public void Initialise(NancyConventions conventions)
            {
                // This is where we register the top level directory containing the module as a root for static content.
                var tlf = Regex.Match(typeof(T).Namespace, "(?<=\\.)[^\\.]*", RegexOptions.Singleline).Value;
                var conv = StaticContentConventionBuilder.AddDirectory(tlf);
                conventions.StaticContentsConventions.Add(conv);
            }
    
            public Tuple<bool, string> Validate(NancyConventions conventions)
            {
                return new Tuple<bool, string>(true, "no problem joe");
            }
        }
    }
    

    这是我的解决方案资源管理器的小屏幕截图,让您感兴趣。由于所有内容都使用相对链接调用,因此可以将所有内容称为“_”。该位置成为标识符。而且它不脆。文件夹可以移动、重命名和嵌套。请注意名称空间对应!南希摇滚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多