How to use RazorPDF in Umbraco 4.11 - asp.net-mvc

I'm using Umbraco 4.11.8 and want to create a PDF.
I have HTML code, that should be downloadable as a PDF
I found a package RazorPDF, which works perfectly in a MVC sample.
But when I try it in Umbraco, I don't know how to get it.
Do I need a SurfaceController or anything else.
If yes, how do I say Umbraco to use my (Surface)Controller?

I put it as answer so it's more readable. Feel free to edit.
I don't find my fault or what I'm missing.
My Controller signature is like this: public class PdfCreatorController : Controller
and the method, which I want to call is called public override ActionResult Pdf().
My routing:
RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);`.
Then I try to call my controller like www.url.com/PdfCreator/Pdf.

The best place to start for what you need is here: http://our.umbraco.org/documentation/Reference/Mvc/
But no, strictly speaking you don't need a SurfaceController. You would only need one if you wanted to access the Umbraco context for the requests you are making via the controller.
So if you just want to have a controller serve up your PDF, you can have a standard controller implementation. You will have to adjust your routes for this though, and this is all covered in the Umbraco documentation above.
Edit:
You'll need a route like this:
RouteTable.Routes.MapRoute(
name: "PdfRazor",
url: "PdfCreator/{action}"
);
You will also need to add the path to the Web.config for Umbraco to ignore, e.g:
<add key="umbracoReservedPaths" value="~/umbraco,~/install,~/pdfcreator" />

Related

How to mask asp.net MVC URL?

I have the following URL:
http://localhost/FolderB/Index
This goes to the following controller:
/FolderB/HomeController.cs
I need to instead use this URL: http://localhost/SubComponent.
I'm not sure how that is done in asp.net MVC. What is this type of masking called and where does the code live to accomplish it?
I have tried the following in RouteConfig.cs but it doesn't work:
routes.MapRoute(
name: "SubComponent",
url: "SubComponent",
defaults: new { controller="Home", action="Index", id=UrlParameter.Optional},
namespaces: new[] { "MyNamespace" }
A simple way of doing this would be to add another route in MVC that routes SubComponent.
You can add a route by looking for where your routes are wired up, which is usually either in Global.asax.cs or in a class called RouteConfig and adding your own route:
routes.MapRoute(name: "SubComponent", url: "SubComponent/{id}", defaults: new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
The order which routes are added matters, so make sure this call to MapRoute occurs before all others.
And specify an area if you need to.
For ASP.NET MVC 5, use RouteAttribute. For older ASP.NET MVC version, you may have to resort to other open source (or your own custom) implementations e.g. https://github.com/mccalltd/AttributeRouting
For Latest Versions:
https://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx
If you are using ASP.NET 3.5 ASP.NET Routing could be a good choice for you.
MSDN page: msdn.microsoft.com/en-us/library/cc668201.aspx
Using it with ASP.NET MVC at ScottGu blog:
weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
Using it with ASP.NET 3.5: www.techbubbles.com/aspnet/aspnet-35-url-routing/
If your website runs under ASP.NET 2.0 Helicon ISAPI Rewrite could be a good choise for you. This is an IIS filter that redirects requests to your pages according to regex-based configuration file. They have a free version for one website.
Have a look at Helicon: www.isapirewrite.com
Hope this helps. Good Luck

make url shorter of some controllers and actions

I would like to make some urls of my asp.net MVC4 app shorter. For example I have Account controller and such action ForgotPassword. Url looks like this
http://www.mydomain.com/account/forgotpassword
I would like to make the url shorter(example below) without renaming actual controller and action names. What is the best way to do that?
http://www.mydomain.com/a/fp
You could register a simple route:
routes.MapRoute(
name: "ForgottenPassword",
url: "a/fp",
defaults: new { controller = "Account", action = "ForgottenPassword" }
);
...in RouteConfig.cs if you're using MVC4.
If i am not wrong, then your talking about Friendly URL's?
Please have a look # quysnhat.wordpress.com
There was a very nice post in Hanselman's web.
Also, there were few questions related to friendly url's(in case it helps you) :-
How can I create a friendly URL in ASP.NET MVC?
http://www.intrepidstudios.com/blog/2009/2/10/function-to-generate-a-url-friendly-string.aspx
The easiest but not the best way of doing it is to hand-code your custom routes:
routes.MapRoute(
name: "AccountForgotRoute",
url: "a/fp/{id}",
defaults: new { controller = "Account", action = "ForgotPassword", id = UrlParameter.Optional }
);
The downside to that is if you will have tons of controllers and action methods and you like all of them to be "shortened", then you will have to write a lot of routes.
One alternative is to define your custom routes in a database and write it out on a file. So for example you have in a database row an accountcontroller-forgotpassword key with a value of a/fp, you dynamically build the route definition, write it in a file and let your application pick it up. How your application can pick up the route definition can be done like this. That link is still applicable for MVC 4. But this one is really messy, IMO, but is an alternative.

How do I create a simple route in MVC3?

I am writing a short url service in MVC3, partly as a learning tool.
When I load the url http://mysite/abc I want to redirect to an action in my controller with the following signature:
public ActionResult RedirectToLink(string shortLink)
How would I create a route in order to run this code? I have tried the following:
routes.MapRoute("Link", "{shortLink}", new { controller = "LinkController", action = "RedirectToLink" });
Alternatively, if someone can point me towards a decent primer for MVC3 that actually covers the basics rather than what's changed since the last version and would cover this scenario, I'd be much obliged.
Thanks
this is the route your want :
routes.MapRoute(
"ShortLink", // Route name
"{shortLink}", // URL with parameters
new { controller = "Link", // Parameter defaults
action = "RedirectToLink",
shortLink= UrlParameter.Optional }
);

ASP.NET MVC Routing Questions

I just started using ASP.NET MVC and I have two routing questions.
How do I set up the following routes
in ASP.NET MVC?
domain.com/about-us/
domain.com/contact-us/
domain.com/staff-bios/
I don't want to have a controller specified in the actual url in order to keep the urls shorter. If the urls looked liked this:
domain.com/company/about-us/
domain.com/company/contact-us/
domain.com/company/staff-bios/
it would make more sense to me as I can add a CompanyController and have ActionResults setup for about-us, contact-us, staff-bios and return appropriate views. What am I missing?
What purpose does the name "Default" name have in the default routing rule in Global.asax? Is it used for anything?
Thank you!
I'll answer your second question first - the "Default" is just a name for the route. This can be used if you ever need to refer to a route by name, such as when you want to do URL generation from a route.
Now, for the URLs that you want to set up, you can bypass the controller parameter as long as you're ok with always specifying the same controller as a default. The route might simply look like this:
{action}/{page}
Make sure that it's declared after your other routes, because this will match a lot of URLs that you don't intend to, so you want the other routes to have a crack at it first. Set it up like so:
routes.MapRoute(null, "{action}/{page}",
new { controller = "CompanyController", action = "Company", page = "contact-us" } );
Of course your action method "Company" in your MyDefault controller would need to have a "string page" parameter, but this should do the trick for you. Your Company method would simply check to see if the View existed for whatever the page parameter was, return a 404 if it didn't, or return the View if it did.
Speaking of setting up routes and Phil Haack, I have found his Route Debugger to be invaluable. It's a great tool for when you don't understand why particular routes are being used in place of others or learning how to set up special routing scenarios (such as the one you've mentioned). It's helped clear up many of the intricacies of route creation for me more than any other resource.
To answer your second question about Global.asax, it an optional file used for responding to application level and session-level events raised by ASP.NET or HTTP modules. The Global.asax file resides in the root directory of the ASP.NET application.If you do not define it assumes you have not defined any application handler or session handler. MVC framework uses the routing engine to which the routing rules are defined in the engine, so as to map incoming URL to the correct controller.
From the controller, you can access the ActionName. If there is no specific controller, it will direct to the default page. The default controller is "Home" with its default action "Index". Refer to MSDN:
http://msdn.microsoft.com/en-us/library/2027ewzw%28v=vs.100%29.aspx
Refer to stackoverflow question
What is global.asax used for?
This is a sample of how a default route should look like
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
);

How do you change the controller text in an ASP.NET MVC URL?

I was recently asked to modify a small asp.net mvc application such that the controler name in the urls contained dashes. For example, where I created a controller named ContactUs with a View named Index and Sent the urls would be http://example.com/ContactUs and http://example.com/ContactUs/Sent. The person who asked me to make the change wants the urls to be http://example/contact-us and http://example.com/contact-us/sent.
I don't believe that I can change the name of the controller because a '-' would be an illegal character in a class name.
I was looking for an attribute that I could apply to the controller class that would let me specify the string the controller would use int the url, but I haven't found one yet.
How can I accomplish this?
Simply change the URL used in the route itself to point to the existing controller. In your Global.asax:
routes.MapRoute(
"Contact Us",
"contact-us/{action}/",
new { controller = "ContactUs", action = "Default" }
);
I don't believe you can change the display name of a controller. In the beta, the controller was created using route data "controller" with a "Controller" suffix. This may have changed in RC/RTM, but I'm not sure.
If you create a custom route of "contact-us/{action}" and specify a default value: new { controller = "ContactUs" } you should get the result you are after.
You need to configure routing. In your Global.asax, do the following:
public static void RegisterRoutes(RouteCollection routes)
{
...
routes.MapRoute(
"route-name", "contact-us/{action}", // specify a propriate route name...
new { controller = "ContactUs", action = "Index" }
);
...
As noted by Richard Szalay, the sent action does not need to be specified. If the url misses the .../sent part, it will default to the Index action.
Note that the order of the routes matter when you add routes to the RouteCollection. The first matched route will be selected, and the rest will be ignored.
One of the ASP.NET MVC developers covers what Iconic is talking about. This was something I was looking at today in fact over at haacked. It's worth checking out for custom routes in your MVC architecture.
EDIT: Ah I see, you could use custom routes but that's probably not the best solution in this case. Unless there's a way of dealing with the {controller} before mapping it? If that were possible then you could replace all "-" characters.

Resources