0

我试图使用一些插件在弹出窗口中显示图像。我试过Thickbox,fancybox,但没有用。

我认为我的网址有问题。

这是我的代码:

<a href='@Url.Action("GetSchemaImage", "Hall", new {folderName = @Model.FolderName })' class="fancybox"><img src='@Url.Action("GetSchemaImage", "Hall", new {folderName = @Model.FolderName })'/></a>

这是控制器的操作:

public ActionResult GetSchemaImage(string folderName)
        {
            if(string.IsNullOrWhiteSpace(folderName))
                return new EmptyResult();
            var folder = ConfigurationManager.AppSettings["SchemasFolder"];
            var path = Path.Combine(folder, folderName);
            var schemaImage = Directory.GetFiles(path, "*.*").FirstOrDefault(s => s.EndsWith(".png") || s.EndsWith(".jpg"));
            if (schemaImage != null) 
                return File(Path.Combine(path, schemaImage), "image/png");
            return new EmptyResult();
        }

这是生成的标记:

<a class="fancybox" href="/Hall/GetSchemaImage?folderName=marineclub"><img src="/Hall/GetSchemaImage?folderName=marineclub"></a>

正如我所说,我认为 URL: 中的问题/Hall/GetSchemaImage?folderName=marineclub。插件无法理解这一点。

我认为 URL 必须如下:/Hall/GetSchemaImage/marineclub。我怎样才能重写我的代码以使 fancybox 可以工作?

这是javascript代码:

$(function () {            
        $(".fancybox").fancybox({ type: image });
})

非常感谢。

更新: 这是所有路线:

public static void RegisterRoutes(RouteCollection routes)
        {
            var entryRoute = new PageEntry("page/{name}/",
                                           new RouteValueDictionary(
                                               new
                                               {
                                                   controller = "DynamicPage",
                                                   action = "Index",
                                                   name = String.Empty
                                               }),
                                               new RouteValueDictionary(new { name = @".+" }),
                                           new MvcRouteHandler());

            routes.Add("display-page",
                       entryRoute);

            routes.MapRoute("Event Overview", "{city}/{type}/{id}",
                            new {city="astana", controller = "BaseEvent", action = "EventOverview"}, new {city = new CityConstraint()});


            routes.MapRoute(
                "Cinema", // Route name
                "{city}/cinema", // URL with parameters
                new { city = "astana", controller = "Cinema", action = "Index" }, // Parameter defaults
                new { city = new CityConstraint() }
            );

            routes.MapRoute(
                "Concert", // Route name
                "{city}/concert", // URL with parameters
                new { city = "astana", controller = "Concert", action = "Index" }, // Parameter defaults
                new { city = new CityConstraint() }
            );

            routes.MapRoute(
                "Club", // Route name
                "{city}/club", // URL with parameters
                new { city = "astana", controller = "Club", action = "Index" }, // Parameter defaults
                new { city = new CityConstraint() }
            );

            routes.MapRoute(
                "Children", // Route name
                "{city}/children", // URL with parameters
                new { city = "astana", controller = "Children", action = "Index" }, // Parameter defaults
                new { city = new CityConstraint() }
            );

            routes.MapRoute(
                "Sport", // Route name
                "{city}/other", // URL with parameters
                new { city = "astana", controller = "Other", action = "Index" }, // Parameter defaults
                new { city = new CityConstraint() }
            );

            routes.MapRoute(
                "Theater", // Route name
                "{city}/theater", // URL with parameters
                new { city = "astana", controller = "Theater", action = "Index" }, // Parameter defaults
                new { city = new CityConstraint() }
            );  


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


            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");



            routes.MapRoute(
                "Activate",
                "Account/Activate/{username}/{key}",
                new
                {
                    controller = "Account",
                    action = "Activate",
                    username = UrlParameter.Optional,
                    key = UrlParameter.Optional
                });

            routes.MapRoute(
               "ResetPassword",
               "Account/Reset/{email}/{key}",
               new
               {
                   controller = "Account",
                   action = "Reset",
                   email = UrlParameter.Optional,
                   key = UrlParameter.Optional
               });
        }
4

1 回答 1

1

因此,如果您想要这样的 URL:/Hall/GetSchemaImage/marineclub您可以做以下两件事之一:

  • 像这样更改您的操作方法,使其与您的默认路由匹配:

    public ActionResult GetSchemaImage(string id)
    

    所以现在Hall/GetSchemaImage/marineclub=>{controller}/{action}/{id}

或者

  • 添加路由以匹配您的控制器:

    routes.MapRoute(
                "ImageStuff", // Route name
                "{controller}/{action}/{folderName}", // URL with parameters
                new { controller = "Hall", action = "GetSchemaImage",
                    folderName = UrlParameter.Optional });
                    //^^ if set optional it will null if not specified.
    
于 2012-05-16T04:38:43.817 回答