c# - 网址没有命中任何 Action

标签 c# asp.net-mvc url-routing kentico

我有以下由 Kentico 创建的用于预览页面的 url:

http://localhost:63603/cmsctx/pv/administrator/culture/en-GB/wg/7c938312-f3b2-4c87-afc3-d6e113d59177/h/8eabc114188f83ff5c6697cf943093ddf80ab319e1312aad7260feabd68acc89/-/about-us?uh=2fb6ea5fcb0f72d0b8a68be30aeea6ca9b985c84d9a5e7a605a746f58f53afae

但它没有影响我的任何 Controller 或操作 - 在我的路由配置中,我有以下 2 条路由:

// Kentico CMS Console Preview Route
routes.MapRoute(
    name: "Preview",
    url: "cmsctx/pv/{username}/culture/{culture}/wg/{workflowGuid}/h/{hash}/{*pathname}",
    defaults: new { controller = "Preview", action = "Index" });

// Default catch all route
routes.MapRoute(
    name: "Default",
    url: "{*pathname}",
    defaults: new { controller = "Alias", action = "CatchAll" });

我希望上面的 url 命中预览 Controller ,或者至少捕获所有,但两个 Controller 都没有被命中,我只是得到一个 404 找不到资源。

奇怪的是,如果我从 url 中删除 /-/ 或者在它旁边放一个字母或数字 /-1/,那么预览 Controller 将被点击.有谁知道为什么 /-/ 导致我的代码没有命中任何 Controller ?

这是我的预览 Controller :

public class PreviewController : Controller
{
    public ActionResult Index(PreviewModel model)
    {
        model = previewModelBuilder.GetPreviewModel(model);

        if (model.Page != null)
        {
            return View(model);
        }
        else
        {
            throw new HttpException(404, "Page not found");
        }
    }
}

最佳答案

好的,我发现这与 Kentico 所做的事情有关 - 在我的 Global.asax 中,我注册了以下内容:

ApplicationConfig.RegisterFeatures(ApplicationBuilder.Current);

这会运行以下内容:

public static void RegisterFeatures(ApplicationBuilder builder)
{
    if (builder != null)
    {
        builder.UsePreview();
    }
}

删除它允许我的路由启动并且 Controller 被击中

关于c# - 网址没有命中任何 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44822489/

相关文章:

c# - 如何定义双键快捷方式?

c# - 使用 TPL 的协作式多任务处理

c# - 使用C#在字符串中的适当位置插入特殊字符

asp.net-mvc - 如何根据枚举值检查单选按钮

c# - 将 FileUpload 方法放在类库中

c# - 在ASP.NET MVC4中,如何重定向到{controller}/{id}?

javascript - 路由在 Angular.js 中不起作用

c# - 如何在两个不同的 .NET Core 应用程序之间共享加密数据?

asp.net-mvc - SignalR 2.0 - IIS 虚拟目录中的 404

php - .htaccess 重写多个参数的规则