Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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 mvc 剃须刀NETMVC3。动态创建页面/内容_Asp.net Mvc_Asp.net Mvc 3_Razor - Fatal编程技术网

Asp.net mvc 剃须刀NETMVC3。动态创建页面/内容

Asp.net mvc 剃须刀NETMVC3。动态创建页面/内容,asp.net-mvc,asp.net-mvc-3,razor,Asp.net Mvc,Asp.net Mvc 3,Razor,对于我来说,问题是理解当页面没有要映射到的控制器/视图时,如何使用uniq URL呈现动态创建的页面 我正在用Razor在ASP.NETMVC33中构建一个CMS系统。在数据库中,我存储页面/站点结构和内容 我想我需要在一个控制器中进行一些渲染操作,以便使用数据库中的内容创建自定义视图?那么URL的呢?我会创建一个单独的文件夹(比如“DynamicContent”之类)来保存这些动态页面,并在Global.asax.cs中添加相应的IgnoreRoute调用RegisterRoutes方法,如下

对于我来说,问题是理解当页面没有要映射到的控制器/视图时,如何使用uniq URL呈现动态创建的页面

我正在用Razor在ASP.NETMVC33中构建一个CMS系统。在数据库中,我存储页面/站点结构和内容


我想我需要在一个控制器中进行一些渲染操作,以便使用数据库中的内容创建自定义视图?那么URL的呢?

我会创建一个单独的文件夹(比如“DynamicContent”之类)来保存这些动态页面,并在Global.asax.cs中添加相应的
IgnoreRoute
调用RegisterRoutes方法,如下所示:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("DynamicContent/{*pathInfo}");

    ...
}
public class DynamicController : Controller
{
    public ActionResult Index(string pathInfo)
    {
        // use pathInfo value to get content from DB
        ...
        // then 
        return new ContentResult { Content = "%HTML/JS/Anything content from database as string here%", ContentType = "%Content type either from database or inferred from file extension%"}
        // or (for images, document files etc.)
        return new FileContentResult(%file content from DB as byte[]%, "%filename to show to client user%");
    }
}
之后,用户将能够使用URL访问这些页面,如

更新

如果您不想在服务器硬盘上放置文件,那么您可以为这些文件创建一个特殊的控制器。这条路线应如下所示:

public static void RegisterRoutes(RouteCollection routes)
{
    ...

    routes.MapRoute(
        "DynamicRoute", // Route name
        "Dynamic/{*pathInfo}", // URL with parameters
        new { controller = "Dynamic", action = "Index"} // Parameter defaults
    );

}
您的DynamicController.cs应如下所示:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("DynamicContent/{*pathInfo}");

    ...
}
public class DynamicController : Controller
{
    public ActionResult Index(string pathInfo)
    {
        // use pathInfo value to get content from DB
        ...
        // then 
        return new ContentResult { Content = "%HTML/JS/Anything content from database as string here%", ContentType = "%Content type either from database or inferred from file extension%"}
        // or (for images, document files etc.)
        return new FileContentResult(%file content from DB as byte[]%, "%filename to show to client user%");
    }
}

请注意,pathInfo之前的星号(*)将使此路由抓取
动态
之后的整个URL部分-因此,如果您已输入,则整个字符串
路径/to/file/something.html将在参数
pathInfo
中传递给DynamicController/Index方法。

谢谢您的回答,但是如果我想知道URL是什么,它将镜像数据库中动态创建的结构。例如:www.mysite.com/about/contact那么about和contact只是数据库中的一个页面结构。我希望你明白我的意思?:)@米克约翰逊:相应地修改了我的答案,希望能有所帮助。