I am looking to do something that might not be possible, but I will leave it up to the community to decide that.
I have a table called content, in this table are the following: Title and Content.
Based on this information I would like the user to be able to input a title and their content and from the title a page is created so they can go to site.com/title and see the content for that page.
I am sure there is more to this, how ever that's where I require your guy's help. I am not looking to create a CMS, instead I am looking to create a piece of a CMS, the "page generation" part.
I don't have any code as this is an idea and I looking for a direction to take to produce the code.
Thoughts?
Create a route with a hardcoded controller that takes a string (the Title) as a parameter. Then in that controller, use the Title to pull the Content out of the database. Pass the Content to the view (ensuring the HTML is sanitized) and display it.
routes.MapRoute(
name: "NotACMS",
url: "{title}",
defaults: new { controller = "NotACMS", action = "Index" }
);
Related
I'm working on a dotnet 6 mvc application using RazorPages, and I'm having a problem with strange routing behavior.
I have a RazorPage /Pages/News.cshtml
This page is accessible using the default route /news
When called without any parameters this page will display an index of news articles.
I also want this page to be able to display a specific news article, via a path like this...
/news/1234-my-news-article
To achieve this, I've added a config like so...
builder.Services.AddRazorPages(options =>
{
options.Conventions.AddPageRoute("/News", "News/{id:regex(^\\d+\\-.*)?}");
});
In my templates, I can then use links like this...
<a asp-page="/News" asp-all-route-data="#(new Dictionary<string, string> { { "id", "1234-my-news-article" } })">My News Article</a>
or
<a asp-page="/News">All Articles</a>
However, once I've navigated to a specific article, the index link doesn't render correctly, and will instead link again to the same article. It appears to be re-using the current routing parameters.
Is there some way to avoid this?
update:
I've found a work-around, if I use this tag instead...
<a asp-page="/News" asp-route-id="">All Articles</a>
then it will link correctly to "/news".
It seems a bit counter-intuitive to have to explicitly set this to blank. I would have assumed it would default to unset, unless explicitly set otherwise.
This is known as ambient route values (https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper#ambient-route-values), where the current route values are automatically added to outbound links which are generated by the anchor tag helper if the destination page is the same as the current page. In older versions of Razor Pages, this was the default behaviour for all pages.
As you have discovered, you can override this by setting the route value to an empty string, or as suggested elsewhere, to use a plain anchor tag for your link.
asp-page tag helper will add id value to the route by default.It is by design.If you don't want to add it,you can try to use href to replace asp-page:
All Articles
If I had a page type called "MySite.TextBox" and it had two fields: "Title" and "Content" what all would be required for me to make a view component that pulls all page types from the site searching from "/" and looking down to have it appear as a list of items in any view, including the layout view, using something like <vc: text-box-list > in a view, such as the _layout
What files would I need to create aside from the TextBoxList view component?
I know I'll need to make a generated code file for the MySite.TextBox page type. But it's where I set up and establish the rules for outputting the list that things get fuzzy.
For instance if I wanted to establish rules like:
var textBoxList = DocumentHelper.GetDocuments("MySite.TextBox").TopN(5).Path("/", PathTypeEnum.Children).OrderByAscending("Title").ToList();
Would that be a part of the view component or something else?
I would just really really love to have an example. I hate that the Dancing Goat and Medio Clinic sites do not have one.
1. Follow the below to Create a widget -
https://docs.xperience.io/developing-websites/page-builder-development/developing-widgets
2. Retrieves pages of multiple page types from the '/' section of the content tree:
var pages = pageRetriever.RetrieveMultiple(multiDocumentQuery => multiDocumentQuery
.Path("/", PathTypeEnum.Children));
Reference:
https://docs.xperience.io/custom-development/working-with-pages-in-the-api#WorkingwithpagesintheAPI-Retrievingpagesonthelivesite
3. Retrieve Common data from multiple page types
var commondata=Pageretriver.RetriveMultiple(multiDocumentQuery=> multiDocumentQuery
.Types("PageType1","PageType2)
.WithCoupledColumns())
.Select(page=> new <desiredobject name>
{
fieldName=page.GetString("fieldName")
});
Reference
https://docs.xperience.io/custom-development/working-with-pages-in-the-api/reference-documentquery-methods
I'm studying codeigniter and I would realize a simple application. I'm asking if every page, even if doesn't not contain directly dynamic element must be create through MVC pattern? I explain myself: my home page will not contain anything of dinamic. only an header, menu and footer. it needs to create model,controller and view to handle this situation or I create simple the home page?
You always have to create a controller because that is what is called from the url.
As far as the view and model. You don't always have to create either.
I've got plenty of pages with static info so I don't need any model interaction at all.
Without a view you are kind of defeating the purpose of the MVC. It is possible for the controller to just echo all your html for the page but I wouldn't do it.
The way I do it is that I have a default view that contains the header and footer. A content view that all my content for the page goes into. I then pump my view for the page into the content view then that into the default view to create my page.
$arrData["vwsContent"] = $this->load->view("your view for the page", $arrData, TRUE);
$arrData["vwsPageContent"] = $this->load->view("content template view", $arrData, TRUE);
$this->load->view("default template view", $arrData, FALSE);
In this way I can have different content views but the same default view for all the pages. For instance my homepage looks different than my regular pages so I would have a HOME template to use instead of a CONTENT template.
You can define the home page function in any controller.
In routes.php the default controller and action can be defined
$route['default_controller'] = "welcome"; (welcome can be replaced by any your prefer controller) .
Create function with name index
function index(){
$this->load->view('index');
}
Then create the file index.php in "views" folder.
In index.php you can put all your HTML static content. You can use URL helper [ function base_url()] for images/css/js path.
Just starting to get the hang of MVC3 and want to start building out some custom controls that I can just drop into a view. Specifically I want to be able to drop in some html form controls that will automatically add some javascript for validation.
something like this is what I'd want:
#Html.VTextBox("MyTextBox","",new {#vType="PhoneNumber", #vMessage="You Must Enter a Phone Number" })
when the page is rendered, I'd want it to search for any custom elements I create then drop in the appropriate javascript to validate client side. I did something very similar to this with asp.net web forms for custom controls ie
<ucc:VTextBox ID="MyTextBox" runat="server" message="You must fill this in" />
just wondering if MVC3 offers the same extensibility. Any resources or advice you can provide would be greatly appreciated.
TIA
To start off with, you'll need to look into extension methods and more specifically, how to create extension methods for the HtmlHelper class so you could write code like the example you've shown above.
For the example you've shown above, you could write code like this:
public MvcHtmlString VTextBox(string name, object value, object htmlAttributes)
{
StringBuilder html = new StringBuilder();
// Build your html however you want
// Here's a simple example that doesn't
// take into account your validation needs
html.AppendFormat("input type=\"text\" name=\"{0}\" value=\"{1}\" />", name, value);
return MvcHtmlString(html.ToString());
}
Then in your view you can use the example above like so:
#Html.VTextBox("MyTextBox","",new {#vType="PhoneNumber", #vMessage="You Must Enter a Phone Number" })
NOTE: You'll need to import the namespace the extension method is in to your view. Simplest method, put a #using ExtensionMethodNamespace at the top of your view. You can make the namespace automatically imported to all your views by fiddling with the web.config (and maybe the Global.asax).
ADDENDUM: Please note RPM1984's comment below where he advises to use TagBuilder in place of StringBuilder which is sound advice since this is the exact scenario TagBuilder was designed for. He also mentions strong typing the helper to the model which is also great advice.
When using the MVC design pattern I usually try to make my view files as simple as possible.
Therefore in my View I try not to have lots of code like this:
if page title exists
display page title
else
display 'default page title'
end if
Instead, in my Controller I might use code like this:
if no page title is specified
page title = 'default page title'
end if
load the view (pass page title as a parameter)
Is this the best way to tackle this issue?
Keeping this if-else condition in the controller level is a better idea than moving into views. (if you can otherwise it's alright)
It will make your views look good !!!!