If you Hijack a route than the Url wil not change.
e.g.
I have a contentnode that makes use of the documenttype "Specialpage". with the url www.site.com/somepage.
If I want to hijack the route I can do so by making a SpecialpageController.cs. (Documenttypealias + Controller).
In that file I can do whatever I need to do.
The Url stays the same but now the code in the controller gets executed as well.
For the model I use the model generated by modelsbuilder and extend it so it will be:
namespace Umbraco.Web.PublishedContentModels
{
public partial class [Modelsbuilder modelname you want to extend]
{
public string MyContent { get; set; }
}
}
I'm not sure what you are trying to do in your controller.
If you want to render the page I'd use:
public class [Modelsbuilder modelname]Controller : RenderMvcController
{
public ActionResult Index([Modelsbuilder modelname] model)
{
//assign value to the custom propert
return CurrentTemplate(model);
}
}
if you want to post something or make a partial action you can use:
public class [Modelsbuilder modelname]SurfaceController : SurfaceController
{
[HttpPost]
public ActionResult index ([custom model] model)
{
//Do stuff
return RedirectToCurrentUmbracoPage();
}
}
namespace MyDomain.Controllers
{
public class MyUmbracoDocTypeController : RenderMvcController
{
public ActionResult MyUmbracoTemplate(RenderModel model)
{
int type = model.Content.DocumentTypeId;
var myCustomModel = new MyViewModel(model.Content) { MyContent = "aa" };
return CurrentTemplate(myCustomModel);
}
}
}
Make sure your controller name matches the alias name you have given your document type, so if your document type has an alias of "articlePage" then your controller must be called "ArticlePageController".
For your controller action, the way I do it is make sure it has the same name as the template, so if your template is "ArticlePage.cshtml" then the controller action should be called "ArticlePage".
For your view model, typically what I do is not derive from the RenderModel base class, return View(model) from my controller action and change the inherits statement of my template to:
Route hijacking: what will be page url?
What would be a page url if I use route hijacking?
Eg. My extended RenderMvcController controller is MyController and my action is MyAction
Will the page url be /MyController/MyAction/ or the url set up in umbraco for that page which is /my-action/?
I'm getting page not found when I'm trying either so am not sure if I'm doing it right.
If you Hijack a route than the Url wil not change.
e.g.
I have a contentnode that makes use of the documenttype "Specialpage". with the url www.site.com/somepage.
If I want to hijack the route I can do so by making a SpecialpageController.cs. (Documenttypealias + Controller). In that file I can do whatever I need to do.
The Url stays the same but now the code in the controller gets executed as well.
More info can be found here
So that's strange because my page url shows page not found and when I try to debug the controller is not hit
Can you show some code?
Model:
Controller:
View:
With the above I'm getting:
which is the umbraco generated url to my page and which was working before I added route hijacking.
I see 2 things that I do differently:
For the model I use the model generated by modelsbuilder and extend it so it will be:
In my view I use:
I'm not sure what you are trying to do in your controller.
If you want to render the page I'd use:
if you want to post something or make a partial action you can use:
Sorry but I don't get what you suggesting:
what is [Modelsbuilder modelname you want to extend]?
Why I place it under Umbraco.Web.PublishedContentModels namespace? And where do I extend it? In my custom model?
What is [ContentModels.SiteContentPage]? - the model type in the view?
What is [Modelsbuilder modelname] in your controller? - is it umbraco template alias or document type alias?
How in the view Model can have Content property if it doesn't take IPublishedContent in constructor?
I simply based my code on this article: https://our.umbraco.org/documentation/reference/routing/custom-controllers
What you suggesting is very different and not fully described so I am not able to replicate the full working code for now.
To clarify my code I could change it to:
(Ignore Index action)
Hi manila.
Make sure your controller name matches the alias name you have given your document type, so if your document type has an alias of "articlePage" then your controller must be called "ArticlePageController".
For your controller action, the way I do it is make sure it has the same name as the template, so if your template is "ArticlePage.cshtml" then the controller action should be called "ArticlePage".
For your view model, typically what I do is not derive from the RenderModel base class, return View(model) from my controller action and change the inherits statement of my template to:
Hope this helps.
That's what I did. Not sure why it wasn't working but I redid the whole thing and now it works without a problem.
Thanks all
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.