{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/Razor/issues/1159","repository_url":"https://api.github.com/repos/aspnet/Razor","labels_url":"https://api.github.com/repos/aspnet/Razor/issues/1159/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Razor/issues/1159/comments","events_url":"https://api.github.com/repos/aspnet/Razor/issues/1159/events","html_url":"https://github.com/aspnet/Razor/issues/1159","id":219040154,"number":1159,"title":"Add support for @namespace to configure the namespace the generated class is in","user":{"login":"DamianEdwards","id":249088,"avatar_url":"https://avatars2.githubusercontent.com/u/249088?v=3","gravatar_id":"","url":"https://api.github.com/users/DamianEdwards","html_url":"https://github.com/DamianEdwards","followers_url":"https://api.github.com/users/DamianEdwards/followers","following_url":"https://api.github.com/users/DamianEdwards/following{/other_user}","gists_url":"https://api.github.com/users/DamianEdwards/gists{/gist_id}","starred_url":"https://api.github.com/users/DamianEdwards/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DamianEdwards/subscriptions","organizations_url":"https://api.github.com/users/DamianEdwards/orgs","repos_url":"https://api.github.com/users/DamianEdwards/repos","events_url":"https://api.github.com/users/DamianEdwards/events{/privacy}","received_events_url":"https://api.github.com/users/DamianEdwards/received_events","type":"User","site_admin":false},"labels":[{"id":74542522,"url":"https://api.github.com/repos/aspnet/Razor/labels/enhancement","name":"enhancement","color":"84b6eb","default":true},{"id":557128795,"url":"https://api.github.com/repos/aspnet/Razor/labels/%C4%B0%C4%B0%20-%20Pages%20Blocker","name":"İİ - Pages Blocker","color":"000000","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2017-04-03T19:42:04Z","updated_at":"2017-04-03T21:03:59Z","closed_at":null,"body":"# The Problem\r\nThe classes that are generated by Razor files are placed in a fixed namespace of `AspNetCore`.\r\n\r\nAs part of Razor Pages, it is idiomatic to specify the page's model using `@model`, e.g.:\r\n``` html\r\n@* ~/Pages/Index.cshtml *@\r\n@page\r\n@model IndexModel\r\n```\r\n\r\nThe class `IndexModel` is specified in a sibling CS file named for the CSHTML file it applies to, e.g. `Index.cshtml.cs`\r\n\r\nIn page-focused layout it's also normal to nest identically named files in sub-folders as the site structure is built up, e.g.\r\n```\r\n~/\r\n  /Pages\r\n    _ViewImports.cshtml\r\n    Index.cshtml\r\n    Index.cshtml.cs <-- namespace App.Pages { public class IndexModel : PageModel {\r\n    /Account\r\n      Index.cshtml\r\n      Index.cshtml.cs <-- namespace App.Pages.Account { public class IndexModel : PageModel {\r\n      /Manage\r\n        Index.cshtml\r\n        Index.cshtml.cs <-- namespace App.Pages.Account.Manage { public class IndexModel : PageModel {\r\n```\r\n\r\nIt's also common to have a `_ViewImports.cshtml` in the pages root to set the default namespace imports for all pages, e.g.:\r\n``` html\r\n@using App\r\n@using App.Pages\r\n```\r\n\r\nYou might also include subsequent `_ViewImports.cshtml` files in the sub-folders to import namespaces for types declared in those sub-folders, e.g.:\r\n``` html\r\n@using App.Pages.Account.Manage\r\n```\r\n\r\nThis results in it being impossible to refer to page models via `@model` **without** using the fully qualified class name (including namespace), as there are multiple namespaces imported where the model class name is present. This isn't ideal as it makes it harder to move the page around independently of its content, such that it breaks if you decide to reorganize the layout.\r\n``` html\r\n@* ~/Pages/Account/Manage/Index.cshtml *@\r\n@page\r\n@model App.Pages.Account.Manage.IndexModel\r\n```\r\n\r\n# The Proposal\r\nLet's introduce support for changing the namespace of the generated view/page class via the `@namespace` directive. This directive would be set in the `_ViewImports.cshtml` of new ASP.NET Core projects (via the project templates) to the application's root namespace by default, much like the default `@using` directives are today.\r\n\r\nThe directive will take a very similar form to the `@using` directive, allowing specification of a namespace following white-space after the directive. At design-time it will be projected to the namespace declaration line in the generated file buffer.\r\n\r\n``` html\r\n@namespace App.Pages\r\n```\r\n\r\nOnly a single instance of the `@namespace` directive is supported, and it can be overridden by more specific `_ViewImports.cshtml` files (more specific based on folder depth, similar to `@model`).\r\n\r\nThe actual namespace used will be the combination of that specified by the closest `@namespace` directive in a `_ViewImports.cshtml` flie, combined with the names of the sub-folders down to the folder containing the page (the path). This way, the namespace will typically only need to be set once for the application in the root `_ViewImports.cshtml` file, e.g.:\r\n\r\n```\r\n~/\r\n  /Pages\r\n    _ViewImports.cshtml <-- @namespace App.Pages\r\n    Index.cshtml <-- generated namespace: App.Pages\r\n    Index.cshtml.cs <-- namespace App.Pages { public class IndexModel : PageModel {\r\n    /Account\r\n      Index.cshtml <-- generated namespace: App.Pages.Account\r\n      Index.cshtml.cs <-- namespace App.Pages.Account { public class IndexModel : PageModel {\r\n      /Manage\r\n        Index.cshtml <-- generated namespace: App.Pages.Account.Manage\r\n        Index.cshtml.cs <-- namespace App.Pages.Account.Manage { public class IndexModel : PageModel {\r\n      /Custom\r\n        _ViewImports.cshtml <-- @namespace Custom\r\n        Index.cshtml <-- generated namespace: Custom\r\n        Index.cshtml.cs <-- namespace Custom { public class IndexModel : PageModel {\r\n```\r\n\r\nIf no `@namespace` is specified in a view/page's directives (inherited or direct), the existing default namespace `AspNetCore` will be used.\r\n"},"comment":{"url":"https://api.github.com/repos/aspnet/Razor/issues/comments/291273428","html_url":"https://github.com/aspnet/Razor/issues/1159#issuecomment-291273428","issue_url":"https://api.github.com/repos/aspnet/Razor/issues/1159","id":291273428,"user":{"login":"rynowak","id":1430011,"avatar_url":"https://avatars2.githubusercontent.com/u/1430011?v=3","gravatar_id":"","url":"https://api.github.com/users/rynowak","html_url":"https://github.com/rynowak","followers_url":"https://api.github.com/users/rynowak/followers","following_url":"https://api.github.com/users/rynowak/following{/other_user}","gists_url":"https://api.github.com/users/rynowak/gists{/gist_id}","starred_url":"https://api.github.com/users/rynowak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rynowak/subscriptions","organizations_url":"https://api.github.com/users/rynowak/orgs","repos_url":"https://api.github.com/users/rynowak/repos","events_url":"https://api.github.com/users/rynowak/events{/privacy}","received_events_url":"https://api.github.com/users/rynowak/received_events","type":"User","site_admin":false},"created_at":"2017-04-03T21:03:59Z","updated_at":"2017-04-03T21:03:59Z","body":"The opt-out would be putting the `@namespace` in your page/view directly, or just not using `@namespace` and thus not taking a dependency on the value that it would configure."}}
{"actor":{"display_login":"rynowak"}}