Piranha CMS - How to view individual posts when Prefixless Permalinks in use - asp.net-mvc-routing

We're using Piranha CMS with the following setting:
<prefixlessPermalinks value="true" />
However this breaks individual posts urls generated using the Permalink helper e.g.
#UI.Permalink(post.PermalinkId)
With prefixlessPermalinks set to False this would normally be the url that is generated: /home/1st-test-blog-entry
With prefixlessPermalinks set to True the url generated becomes:
/1st-test-blog-entry (the "home" has been removed as it's prefixless)
Following a prefixless url generates the following exception:
System.Web.HttpException: Cannot use a leading .. to exit above the
top directory.
Any ideas how to circumnavigate this issue?
Attempted workarounds:
I manually prefix "/post" to the permalink generated then I added the following route mapping which catches the request:
routes.MapRoute(
name: "Post",
url: "post/{permalink}",
defaults: new { controller = "Post", action = "Index", permalink = UrlParameter.Optional },
namespaces: new[] { "Maps.Portal.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
The following controller catches the request:
public ActionResult Index(string permalink)
{
var model = GetModel(permalink);
return View(model.GetView(), model);
}
But fails as the readonly SinglePostController.CurrentPermalink property isn't being populated and so causes Piranha CMS to throw an exception. It may be interesting to note that this controller fires correctly when the draft version of the post is being viewed as the CurrentPermalink is being populated by the CMS.

After experimentation I can confirm that the issue isn't with Piranha CMS, but with the MVC shared layout. Still trying to track it down, but stripping out everything apart from the CMS content allows the post page to be shown.
Update:
I finally tracked it down to multiple uses of #Url.Content(url). Very bizarre as all seem to be valid urls e.g. #Url.Content("~/Content/images/ios/57x57iOS.png"). Once these were all removed then the page would render correctly!?
Discovery:
This issue turned out to be a development environment only issue for me...
Running the site in Visual Studio Development Server from VS2012 results in this error, but once the web application was deployed to IIS8 the issue disappeared.

I can't reproduce your error with either MVC or Web Pages, in fact we use prefixless permalinks in almost all live installations I've deployed. I started up the MVC template, installed a fresh database and added a post with your specified title/permalink and it shows up without issues with prefixless permalinks.
How have you set up your installation?
Regards
/HÃ¥kan

Related

Running both aspx and .Net Core (.Net 5) webpages on same server and domain

I am about to move a webshop from old webforms to new .Net Core (.Net 5) site.
The old urls have a lot of good rating on google and I would like to be able to make a permanent redirect from an aspx page to .net core page.
Etc.:
www.mydomain.com/products/great-backpack-1234.aspx
to
www.mydomain.com/great-backpack
I believe that requires that I have both .net core and aspx site running on the same domain.
Is this possible?
Or do any one have any other solution ideas?
Best regards
Thats probably not needed to run both on the same domain or space. It seems everything will end in .aspx, and assuming you are using MVC, a controller function to catch-all requests that don't match other routes, then check to see if it ends in aspx, should do it for you. You can put it in an existing controller, but I personally would put it in a new one to separate concerns
public class RedirectController : Controller
{
[Route("{**url}", Order = 999)]
public IActionResult RedirectPage(string url)
{
//Redirect logic based on the URL given, making sure it ends in aspx
}
}
The url would come across in the url parameter as a string and you can check to see if it ends in aspx, and what the page name was to determine where to send them. So "www.mydomain.com/products/great-backpack-1234.aspx" would come across in the url parameter as "products/great-backpack-1234.aspx". Note that the order field in the route attribute assures that it will be the last match checked for patterns, so it shouldn't mess with anything else.

Embedd Swagger UI into Blazor Server Side App

I would like to embed the swagger UI in a page on my blazor app. I am having a hard time finding examples of how to do this with razor pages. Does anyone have any examples doing this? I am usually more of a back end developer so struggling a bit getting this front end up and running.
Although this is quite an old issue, I've decided to try and find the answer as I wanted to embed the Swagger UI into one of my pages myself.
It turns out that it is quite easy to do it!
All credits go to this repository - owner 'jsauve'.
He shows that you can do it:
Here are the steps that you need to do in order for it to show on your page:
Install Swashbuckle.AspNetCore on your Blazor Server.
In Startup.cs, ConfigureServices add:
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
In Startup.cs, Configure add:
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
});
Simply, create Razor Component and add:
#page "/test"
<iframe src="swagger" style="width:90%;height:1200px;border:none;" />
Navigate to /test from your e.g. MainLayout.razor. I hope it can help somebody because it certainly helped me.

Aqueduct template db_and_auth not rendering login form

I am working on setting up OAuth 2.0 for aqueduct and want to start with a working example. The template db_and_auth is suppose to serve this purpose.
I have followed the readme instructions and connected my database and updated the migration. also I have registered OAuth 2.0 clients in the application database as the readme describes.
The template comes with a route in the channel.dart
router
.route("/auth/form")
.link(() => AuthRedirectController(authServer, delegate: this));
The AuthRedirectController is set up to render a html page (web/login.html) which is part of the template.
However every time I send a get request to this route it returns with a 400 error saying Error
unsupported_response_type in the browser. Additionally in the readme file it only shows testing auth end points using curl. Why would this template include a rendered login page without mention of it in the readme? Any help with this would be much appreciated.
The login form will only be rendered when you pass the correct response_type like so:
http://localhost:8888/auth/form?response_type=token

Use RESTful URLs in a non-MVC site

I started the site using AST.NET Razor template, not ASP.NET MVC template.
I recall seeing somewhere on the Internet that even without MVC, it's possible to use RESTFul URLs in the razor-based ASP.NET site. It appears to work without the CHTML extension names right out of the box -- www.test.com/car automatically redirects to www.test.com/car.cshtml.
But, what if I used www.test.com/car/2, how would I get to the "2" inside the View without using MVC? I really hope that's something already baked in.
Found it -- it's in UrlData
http://beta.asp.net/web-pages/tutorials/aspnet-razor-pages/18-customizing-site-wide-behavior
section "How Routing Works"
Look at the WebGet Attribute. It has a UriTemplate.
Example:
WebGet(UriTempate="{Id}")<br>
public JsonResult Get(int Id)
{
}

Application doesn't read action result method in MVC3

We are using MVC3 and we are trying to create a method in a Controller named UserSesionManager. This method is called from
#using (Html.BeginForm("GetStatTypesDistribution", "UserSesionManager", FormMethod.Post, new { enctype = "multipart/form-data" }))
In the UserSesionmanager Controller we have:
[HttpPost]
public ActionResult GetStatTypesDistribution(FormCollection form)
However when we call it we get this error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /UserSesionManager/GetStatTypesDistribution
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
Also, whe I add a breakpoint it says that the sourcecode is different form the original version. I follow the instructions of right clicking on location but, the error still exists.
Why is this happening, and how can we fix this?
Thanks a lot!
Make sure that the controller is named UserSesionManagerController and not only UserSesionManager:
public class UserSesionManagerController: Controller
{
...
[HttpPost]
public ActionResult GetStatTypesDistribution(FormCollection form)
{
...
}
}
Also make sure that you have a default route in global.asax:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Rebuild your solution. Stop the web server you are using for debugging and check to ensure if you are using the built in web server (Cassini) it's not running in the system tray.
F5 in visual studio to debug. If you still get the error load up the modules window from debug menu then modules. Find your code listed in there an look where it's loaded from.
If it's still an issue delete the apps folder from the temporary asp.net files folder and try debugging again but the modules window should give some info.