Rebuilt My .ASPX Website But How to Easily Redirect to .NET Core Pages [on Shared Hosting]? - asp.net

I know this has been asked before but I haven't seen any simple explanations and I should also add I'm on shared hosting (Plesk). I don't see the URLRewriter utility installed on the server.
Anyway, I rebuilt my 2013 website that did use ASP.NET web forms (with .ASPX extensions). I'd like to be able to redirect my old pages to their new equivalents. i.e.
https://www.findaforum.net/diyfishkeepers-com.aspx
Should now point to:
https://www.findaforum.net/Forums/diyfishkeepers-com/
At the moment the .ASPX pages show this in a red box on a yellow screen:
XML Parsing Error: no element found
Location: https://www.findaforum.net/diyfishkeepers-com.aspx
Line Number 1, Column 1:
Where does this "come" from?
Incidentally, I'm looking for a quick and easy fix because I don't have too many external links pointing to my site's subpages, but it would be nicer for the user experience to fix it while Google works out I've changed my entire site.

This answer basically works:
How to redirect .ASPX pages to .NET Core Razor page
There's a good link to the URL rewriting regular expressions here: https://isitoktocode.com/post/introduction-to-url-rewriting-using-iis-url-rewrite-module-and-regular-expressions
This is what I've put in Startup.cs:
var options = new RewriteOptions()
.AddRedirect(#"ShowCategories.aspx", "/Home/Categories/")
.AddRedirect(#"Statistics.aspx", "/Home/TopForums/")
.AddRedirect(#"SubmitForum.aspx", "/Home/Submit/")
.AddRedirect(#"Help.aspx", "/Home/HelpAndFAQ/")
.AddRedirect(#"([0-9a-z-A-Z]+)(.aspx)", "/Forums/$1/");
app.UseRewriter(options);
Note: Make sure you put the specific ones at the top, then the generic ones at the bottom.
I also had some URLs like https://www.findaforum.net/lpsg-com where 'lpsg-com' is a forum name. I added a URL to the home controller to take care of these...
[HttpGet]
[Route("{code}")]
public ActionResult Unknown(string code)
{
return RedirectToAction(code, "Forums");
}

Related

Getting URL of published element in SDL Tridion

Is there any way of finding the absolute URL for a published object in the SDL Tridion Interface?
For example when I published a page, how can I find the url where to access the page?
Though not finished, and not really very documented, the Tridion 2011 PowerTools includes 2 buttons to "Open in Staging" and "Open in Live".
If you're looking for the code in your c# tbb library you can use the PublishLocationUrl property for pages and structure groups:
StructureGroup.PublishLocationUrl or
Page.PublishLocationUrl
This will return the URL if the item is published or not, as Page and StructureGroup extend the ReposityObject class, I typically perform a check to see if the ReposityObject is published to the target that the Page is being published to for example:
if (PublishEngine.IsPublished(myReposityObject, myEngine.PublishingContext.PublicationTarget))
{
// page or sg is published!
}
Note: Where the myEngine is an instance of the Engine object.
If you're doing this in the core service, that's a little different, what you need to do is create a PublishLocationInfo object which is casted from your Page or StructureGroup object property LocationInfo, as shown below:
PublishLocationInfo pubInfo = (PublishLocationInfo)page.LocationInfo;
return pubInfo.PublishLocationUrl;
It is not very straightforward, mostly because Tridion allows you to publish a single page to multiple targets (= web sites). The page could in fact have a number of URLs.
However, the best option is to open the page and click on the Info tab. There you will find the File Path, which might look like this: \about\press\2011. Replace the backslashes with slashes, and add the page's filename and file extension (can be found on the General tab). Put the whole thing behind the root URL of your web site (e.g. http://www.mysite.com').
Tridion exposes the path of the URL in PublishLocationUrl property. You can access this either through the TOM.NET API or by viewing the raw XML of the item by entering the TCMURI in the address bar of Internet Explorer (e.g. tcm:4-264-64).
But in either case those will just return the path part of the URL. You'll have to prefix it with the correct base URL as Quirijn already mentioned earlier.
In the past, I have resorted to extending the protocol schemas for publication target destinations. Having added a baseURL property there, I could access this from events system code (the idea was to mail a link to a workflow approver).
These days, you could use application data to do the same thing.

ASP.NET MVC 3, IIS7, 404 Error, not routing correctly?

I'm newish to ASP.NET MVC 3, so sorry if I'm my details are slightly murky. I'm trying to publish my web app to a server here. I'm running in IIS 7, set to integrated, and I can get to the home page of the app just fine. However, certain links remove the directory out of the url. Example:
URL of home page:
http://localhost/CMS/ - this will take you to the first screen, with links to "Contract," "Customer," and "Employee." Clicking one of those brings you to...
http://localhost/CMS/Contract (or whichever you choose.) From there, it's broken down into different categories. One of them is "Create Contract." Here's the problem I'm having: that URL points to
http://localhost/Contract/Create - completely omitting the CMS part out and throwing a 404. I can reach it by inserting CMS back inside, and those pages route correctly.
What could be wrong? Let me know if you need more information on any of my code or whatever.
You can define an alternate controller in the route than what you would expect
routes.MapRoute("Contract", "Contract/{action}",
new { controller = "cms", action = "index" }
);
and you should be constructing your links like this within your pages
<%=Html.ActionLink("Contract", "create", "cms") %>
rather than doing it the old fashioned way like
Contracts
which side steps routing.
It sounds like you don't need additional routes but need to create ActionLinks propery using the HtmlHelper
When you are using your paths to to the controller actions you need to use #Url.Action("action", "controller"); instead of just using "action\controller".
See an example http://codetuner.blogspot.com/2011/07/jquery-post-url-problems-in-iis-hosted.html

Image disappear when url route

I am writing a web page to show image (image is dynamically generate by .Net charting) in a web. I have used the asp.net web forms URL routing to navigate to this page. Once I use the URL routing the image appear on the page. Anyway this is working fine for normal page browse.
More than likely your link to the image is using a relative path, and once you introduce routing you are working with a URL structure that appears to be deeper nested in the folder structure than it is.
When linking to the image for display I would recommend using root relative path something like /Images/MyFile.jpg rather than ../Images/MyFile.jpg, or similar.
This way if your route changes, and additional "folders" appear in the route, the link will still work.

asp.net mvc how to manage urls/links and routing centrally (c# + js)

I keep running into problems with URLs and routing.
Couldn't find an answer on SO.
I would like to manage all of my urls/links in a single place.
This is for my C# MVC code and the js/jquery ajax code.
These urls are scattered throughout my application.
Moving to a production server needs some fixes and I don't like the fact that I need to look for all of the occurrences in the application.
I don't mind fixing this once - but I would like to do it only once.
Any ideas how to manage all of these links/urls as a group will be very appreciated.
Be happy ad enjoy life, Julian
Consider using T4MVC
You could use Html.ActionLink or
Html.BuildUrlFromExpression(c => c.ControllerAction())
Depends, if you have application reading off certain urls and those urls changed once in a while. then you might want to consider putting all those urls into a database table/etc and retrieve them using specific key.
that way, when your url changed, all you need to do is to change the url on your database and all your application will still be running fine.
Urls should be managed in a single place: the RegisterRoutes static method in Global.asax. In absolutely every other part of your application you should use Html helpers when dealing/generating urls. This way you will never have problems because helpers take into account your routing system.
So instead of writing:
$('#foo').click(function() {
$('#result').load('/mycontroller/myaction');
return false;
});
you use an HTML helper to generate this foo:
<%: Html.Action("foo", "myaction", "mycontroller") %>
and then:
$('#foo').click(function() {
$('#result').load(this.href);
return false;
});
Never hardcode a single url in your application except of course in global.asax which is the only centralized place urls should be defined. So basically every time you find yourself writing something of the form /foo/bar in some other part than global.asax you are doing it wrong.

Export ASPX to HTML

We're building a CMS. The site will be built and managed by the users in aspx pages, but we would like to create a static site of HTML's.
The way we're doing it now is with code I found here that overloads the Render method in the Aspx Page and writes the HTML string to a file. This works fine for a single page, but the thing with our CMS is that we want to automatically create a few HTML pages for a site right from the start, even before the creator has edited anything in the system.
Does anyone know of any way to do this?
I seem to have found the solution for my problemby using the Server.Ecxcute method.
I found an article that demonstared the use of it:
TextWriter textWriter = new StringWriter();
Server.Execute("myOtherPage.aspx", textWriter);
Then I do a few maniulatons on the textWriter, and insert it into an html file. Et voila! It works!
Calling the Render method is still pretty simple. Just create an instance of your page, create a stub WebContext along with the WebRequest object, and call the Render method of the page. You are then free to do whatever you want with the results.
Alternatively, write a little curl or wget script to download and store whichever pages you want to make static.
You could use wget (a command line tool) to recursively query each page and save them to html files. It would update all necessary links in the resulting html to reference .html files instead of .aspx. This way, you can code all your site as if you were using server-generated pages (easier to test), and then convert it to static pages.
If you need static HTML for performance reasons only, my preference would be to use ASP.Net output caching.
I recommend you do this a very simple way and don't do it in code. It will allow your CMS code to do what the CMS code should do and will keep it as simple as possible.
Use a product such as HTTrack. It calls itself a "website copier". It crawls a site and creates html output. It is fast and free. You can just have it run at whatever frequency you think is best.
It decouples your HTML output needs from your CMS design and implementation. It reduces complexity and gives you some flexibility in how you output the HTML without introducing failure points in your CMS code.
#ckarras: I would rather not use an external tool, because I want the HTML pages to be created programmatically and not manually.
#jttraino: I don't have a time interval in which the site needs to be outputted- the uotput has to occur when a user creates a new site.
#Frank Krueger: I don't really understand how to create an instance of my page using WebContext and WebRequest.
I searched for "wget" in searchdotnet, and got to a post about a .net class called WebClient. It seems to do what I want if I use the DownloadString() method - gets a string from a specific url. The problem is that because our CMS needs to be logged in to, when the method tries to reach the page it's thrown to the login page, and therefore returns the login.aspx HTML...
Any thoughts as to how I can continue from here?

Resources