Razor Page Routing with Friendly Routes not responding - razor

Following https://www.learnrazorpages.com/razor-pages/routing and "Friendly Routes"
With this folder structure:
~Pages / (or "Areas" in last example)
External
PageA
Index.cshtml
Internal
PageB
Index.cshtml
And then I want to add these routes
services.AddMvc()
.AddRazorPages(options =>
{
// Even though above guide states this route it...
// throws exception for "External/PageA" so I have to set "/External/PageA"
options.Conventions.AddPageRoute("External/PageA", "/A");
// throws exception for "External/PageB" so I have to set "/External/PageB"
options.Conventions.AddPageRoute("Internal/PageB", "/B");
});
But all I'm getting is 404 not found. I've tried multilple combinations. I've even followed the Area setup and moved it to areas with the same structure inside the Area folder:
services.AddMvc()
.AddRazorPages(options =>
{
options.AllowAreas = true;
// Also tried "PageA" and "/PageA", "A" and "/A"
options.Conventions.AddAreaPageRoute("External", "/PageA", "/A");
// Also tried "PageB" and "/PageB", "B" and "/B"
options.Conventions.AddAreaPageRoute("Internal", "/PageB", "/B");
});
It's more like the whole convention setup is totally ignored since there has been 0% progress.
The only way that I can access the page is by writing the full folder name. But I don't want this. I want a cleaner routing structure than the folder structure which at the moment seems impossible.
Any suggestions - any ideas?

From the page you linked to:
The [AddPageRoute] method takes two parameters. The first is the relative path to the Razor page file without the extension
You are providing the path to the folder containing the page. You need to add "/Index" to the end of the first argument:
options.Conventions.AddPageRoute("/External/PageA/Index", "A");
The same is true when working with areas. You need to provide the relative path to the actual page, not the folder name:
options.Conventions.AddAreaPageRoute("External", "/PageA/Index", "/A");
This assumes a structure as follows:
Areas
External
Pages
PageA
Index.cshtml

Related

TemplateDoesNotExist at /welcome_page/

I'm new in coding field. I decided to start a project with Django & Python, but I got stuck due to some errors. For the past 3 weeks, I tried to figure out what was the issue but couldn't find it. I will appreciate it if anyone help.
when I run my code, I get as an error, "TemplateDoesNotExist at /welcome_page/"
Everything as been specified but I'm still getting the same error
Here is the error page:
TemplateDoesNotExist at /welcome_page/
content of the welcome page:
Content of my the welcome page
my URLs :
URLS where I defined welcome page
My base content:
My base content
the place where the welcome page is calling from:
The place where the welcome page is calling from
My root to the welcome page from my C: drive:
My root to the welcome page from my C: drive
In your logout_request() view the last line is this:
return redirect("templates/welcomepage.html")
That's trying to redirect the user to a template. It should be redirecting to a URL.
In your urls.py the Welcome Page view has name="welcomepage" - this is what you use to refer to that URL. So change that line in your view to:
return redirect("welcomepage")
That will return the user to the "welcomepage" URL, which uses the views.welcome_page view, and the templates/welcome_page.html template.
By the way, if your welcome_page view is a class, as opposed to a function (I can't see it in your screenshot, so can't tell) then it's more normal in python to capitalise it: WelcomePage. Or WelcomePageView. Functions are lowercase (welcome_page).
If I see it right, you redirect to /templates/xxx.html but that path is not defined in your paths. The templates directory is the internal location, but the user can only see what is defined in the paths.
You should also better redirect ti the name of the page defined in the paths. Please post your settings.py maybe there is also a problem with the search path for templates.

Images in html for Spring thymeleaf won't show up

Below, I am using login.html as the page where I am adding the image test.png within /static/images/
So, in login.html, I have <img src="../static/images/test.png" width="1000" th:src="#{images/test.png}"/>, which gives a blank image. Why isn't it showing up?
In my SecurityConfiguration.java file, I have
#Override
protected void configure(final HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
//.antMatchers("/**").hasRole("ADMIN")
.antMatchers("/static").permitAll()
.and().formLogin()
.loginPage("/login")
.permitAll();
When I use this configuration, it uses the default index.html page which shows the image fine. But, If I uncomment .antMatchers("/**").hasRole("ADMIN"), it will bring me to login.html, but I can't view the image.
There are 3 problems I can spot.
Instead of .antMatchers("/static") you should rather have .antMatchers("/images/**") since anything from src/main/resources/static will be served from the root of your application (as explained here - mind that folders "public" and "static" are interchangeable to Spring Boot).
Order of matchers for .authorizeRequests() matters! Just look as last example of method's documentation. You should have your ant matchers reversed:
.antMatchers("/images/**").permitAll() // more detailed paths should go first
.antMatchers("/**").hasRole("ADMIN") // more general paths should go last
Consider using th:src="#{/images/test.png}" instead of th:src="#{images/test.png}". The extra slash at beginning makes the path relative to the root of your application what gets along with first advice. As stated in Thymeleaf's documentation:
Relative URLs starting with / (eg: /order/details) will be automatically prefixed by the application context name.

Will html image paths still work after precompile?

I'm building a Rails app, but I'm using a plugin in which I have to render my images using only html.
Since I haven't deployed yet, all my images are in RAILS_ROOT/app/assets/images/, so to render an image I have to write the following code:
<img src="/assets/image.jpg">
But when I'm ready to deploy to the web and I perform a precompile, all my images are supposedly going to be moved to my public folder. Will the html still work to link to the image, or will I have to change to link to a different path?
The plugin I'm using is Typeahead:
application.html.erb*
<script type="text/javascript">
//....
$('#typeahead').typeahead(null, {
maxLength: 5,
displayKey: function(thing) {
return "<div class='typeahead'><img src='" + thing.image_url + "'></div>";
},
source: bloodhound.ttAdapter(),
});
</script>
things_controller.rb
def typeahead
#render json: Thing.where(name: params[:query])
q = params[:query]
render json: Thing.where('name LIKE ?', "%#{q}%")
end
*Thing.image_tag is currently set to "/assets/[image.jpg]", except for each thing it's adjusted with the proper file name.
Not only are they going to be in the public folder, but they'll be renamed to include the fingerprint.
You must use the Rails helpers for all assets, see how to here and read the rest of the guide while you're at it :)
I think you should use non-stupid-digest-assets gem as it copies all your assets(mentioned in assets precompile list) in public/assets folder and then you need not to change your code before/after compiling.To install, you just need to add it into your Gemfile.
gem 'non-stupid-digest-assets'
I hope it might help you.
Joe, my suggestion would be to create a directory in your public folder to house your images, instead us using the app/assets directory. The public folder will allow the assets to not be altered by the rails pipeline, and you can link to them reliably using any external services that need the images.
As stated in RailsGuides:
Assets can still be placed in the public hierarchy. Any assets under
public will be served as static files by the application or web server
when config.serve_static_files is set to true. You should use
app/assets for files that must undergo some pre-processing before they
are served.
So you would need to add this line in config/application.rb
config.serve_static_files = true
As described in Rails general configuration.
It looks like you're storing your image_url in your model, and that's not working because assets don't have fixed URLs in Rails. I would override the getter in your model to use the asset_path helper, so it translates the path when that attribute is read (e.g., when the JSON is generated).
Something like:
# thing.rb
[...]
def image_url
ActionController::Base.helpers.asset_path(read_attribute(:image_url))
end
[...]
Short answer, no.
But it isn't that big a deal to remedy. Just move the images you need to reference with html into your Public folder. Then you can simply reference them with this code:
<img src="/image_name.image_type">
and the html will link to the correct path, both before and after precompile. So you don't have to change any code before you deploy.
BTW: I assume image_tag and image_url are the same column and you just made a mistake in one of the two times you mentioned it. If that's the case, then don't forget to change it to simply "/[image.jpg]".

Ajax URL broken

I have an javascript setinterval which runs every 2 minutes to get latest feeds. However, this only work on the index page. The script is in a js file which I included in the main layout page of the site. What could be the cause? I know it has to do with the path, because when I check on Net tab in Firebug, the path is wrong. However, the file is included in the main layout, and every page has it(the layout).
Dont know if it will help, but my script is:
$(document).ready(function(){
setInterval(myfx, my_time);
}
myfx(){
ajax({ url: "mypage", ..)};
}
I think the path is relative to the 'folder' that the user is currently in on the site,.. which is what is causing problems. Thanks
The problem is that your URL is relative, which means the target that you're querying changes as you change pages. I.e. if you're at http://website.com/ then mypage is http://website.com/mypage, but if you're at http://website.com/help/details then mypage is at http://website.com/help/mypage, which probably doesn't exist.
The fix is to make your url absolute (start it with '/', e.g. /mypage) so that it always points to the same location.

How can I configure my route.rb page to redictect to a view

In my route.rb file I currently have:
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.root :controller => "test"
how do I direct my index page to direct to something like this:
http://site.com/railstest/test/index.html
or just:
http://site.com/railstest/test.erb
originally it started off at:
http://site.com/railstest/
which took me to the default html page, which has now been deleted. should I change the route or create a test.rb in the view folder, thank you
It's not possible to make a route that goes directly to a view, bypassing every controller.
So you have two options available to you now:
make it as a static html page
create the full html page
save it in /public/railstest/filename.html
use the URL: site.com/railstest/filename.html
note: hard to maintain, and cannot take advantage of layouts.
create a controller that will serve your view
either "script/generate RailstestController index" of "rails g PagesController index"
delete the app/views/index.html.erb that it creates and copy yourview file to that name instead
should Just Work