Umbraco AJAX call not working - ajax

I have a MVC application where I have installed umbraco 6.1.6 nuget package.Now I am trying to call a controller from a view using Jquery AJAX function. The controller is placed in MVC controller folder and it is inherited with UmbracoApiController. I have tried to follow the Umbraco API document ,But everytime my request is send it redirects to 404 page. I have tried the same using umbraco 7.1.4 downloaded from here and it works fine for me. Can anyone suggest a solution for this.
The request url I have used is as follows :- /Umbraco/Api/[ControllerName]/[ActionName], I have also tried /Umbraco/[YourAreaName]/[YourControllerName] , but that too doesn't work for me.

Check different things:
reboot the site (touch the web.config)
did you BUILD the code? If you put it in app_code is it picked up automatically, otherwise you need the DLL to be in your bin directory.
what do the log files in /App_Data/Logs tell you?
You can insert your own logs by invoking
LogHelper.Info<myclass>("some message")
are you really really really sure you have not overlooked something from the documentation?
are you sure your controller inherits from UmbracoApiController
have you tried adding [MemberAuthorize(AllowAll = true)] to the api controller or the method?
double check the URL. you probably mistyped the name. The controller is the complete name without "controller". Try the code below to find back the exact url.
The code below came from a usercontrol which i used in a dashboard for the end user. I guess it's pretty easy to convert it into a Razor macro. If this is not returning a thing, you did something wrong from the list above.
var requestHandler = HttpContext.Current.Request.RequestContext;
var urlHelper = new UrlHelper(requestHandler);
var actionlink = Umbraco.Web.UrlHelperExtensions.GetUmbracoApiService<MyApiController>(urlHelper, "MyMethodName");

Related

Secure files from being downloaded by using the absolute path in the URL

in my MVC 3 project I have a folder in the project's root where I store some SWF files. The problem is, when I hit the url in the browser's address bar, e.g
localhost:39217/Files/fg/f_l1.swf
obviously I see the download dialog. Is there any way to prevent it ? In the other words, that file would be visible in my page after the DOM is loaded, but if I just type its URL I don't want it to be downloaded. I'm afraid that both scenarios are threated the same in the IIS. Any ideas ?
One way I can see to solve this issue is don't reveal the real physical path to the user. Basically you should deliver the SWF files from a controller action.
If you are embedding the SWF file through object tag then the object tag will refer to this action passing the filename. You can control the action by Authorize attribute or some other ways and once you see the request is properly authorized then you write the flash file into the response.
The idea is clearly explained here though the code is in PHP you can migrate that to MVC.
UPDATE:
If you don't want to change the SWF file path then you have to do little more work in Global.asax.cs.
routes.IgnoreRoute("Javascript/{*catchall}");
routes.IgnoreRoute("Content/{*catchall}");
routes.IgnoreRoute("Scripts/{*catchall}");
routes.RouteExistingFiles = true;
routes.MapRoute("", "Files/Flash/{file}", new { controller = "File", action = "Flash" });
Now eventhough some one tries to access the SWF file directly knowing the path, the requests are handled by the Flash action of File controller and there you can do the necessary auth. check before sending back the SWF.

MVC3 Reverse Routing

I'm working on a project converting older .aspx pages to newer MVC pages. I'm currently using the routing function to map the existing legacy pages to use the newer MVC pages behind the scenes. This is working great because it is preserving the existing URL, however, whenever the controller needs to redirect the user OR an MVC Url.Action link is used, the URL it sends the user to is the MVC url and not the routed legacy page url. I realize that this is how its suppose to be functioning out of the box but I was wondering if MVC has "reverse routing" functionality of some sort. By reverse routing I am looking for something that allows the Url.Action("action") to return the routed .aspx associated url with the specified action instead of the default /controller/action link.
So for instance,
here is a sample route
context.MapRoute("AboutUs", "pages/aboutus.aspx", new { controller = "default", action = "aboutus" });
This enables domain.com/pages/aboutus.aspx to run the new MVC action "aboutus" from the DefaultController behind the scenes perfectly.
However, if on another MVC page I have a link created by
Url.Action("aboutus")
it points them to domain.com/mvc/aboutus. I would like it to point them to domain.com/pages/aboutus.aspx to keep uniformity across the site.
Thanks for any insights that you can provide. I might end up having to override the Url.Action method to look up the route and return the mapped url but I just wanted to check for existing functionality so I don't reinvent the wheel.
Try using the RouteUrl extension method, which matches the route by route name rather than route parameters, like so:
Url.RouteUrl("aboutus") // route name
MSDN: http://msdn.microsoft.com/en-us/library/dd460347

MVC3 and IIS6 RedirectToAction redirects to wrong URL

This is weird. I have a virtual directory setup for an MVC3 application called (for the sake of this question) I'll call 'foobar'. The full URL to this site is:
http://localservername.domainname.com/foobar
In my logon form, I have the following line that is supposed to redirect to the main/home page of the application after logon.
return RedirectToAction("Index", "Home");
However, when this line executes, I get redirected to the wrong location - so instead of redirecting me back to:
http://localservername.domainname.com/foobar
I get redirected back to:
http://localservername.domainname.com/foobar/foobar
Which of course gets me a resource not found error. Also, any links in my views seem to do the opposite - for example if I have a link like this:
Widget Search
I would expect the following URL to open:
http://localservername.domainname.com/foobar/WidgetSearch
Instead, I get redirected to
http://localservername.domainname.com/WidgetSearch
Which of course also gets me a resource not found error. I've never encountered this type of behaviour before. I've gone over the basic and advanced settings and created a new application pool. Fiddler also shows me that (of course) the URLs listed above return 404 responses.
Any suggestions would be greatly appreciated.
Thanks!
The first issue shouldn't really happen. RedirectToAction should take into account your virtual directory path (you haven't hard coded an extra /foobar into your route setup have you?).
The second problem has nothing to do with MVC, it's just that you're using a vanilla HTML link that is pointing directly to the root of the server (that's what saying /blah implies). You should change your link to use one of the MVC helper methods to generate the URL instead to make sure it adds the virtual directory for you. So one of the following (the first is probably the best way, unless you need to heavily customise what the anchor tag looks like):
#Html.ActionLink("Widget Search", "Index", "WidgetSearch")
or
Widget Search

MVC 3 Duplicated Controller names in AJAX post

If from the page localhost:nnnn/Class I click on an AJAX link that will post to 'Class/AddClass' I get a RawUrl of Class/AddClass and it works just fine.
If from the page localhost:nnnn/Class/Index I click the same link I get a RawUrl of Class/Class/AddClass and it (obviously) doesn't work.
I realize I'm in Routing Hell, but who's rewriting the URL and why? I painstakingly stepped through the jQuery code and indeed it's posting to Class/AddClass.
Thanks for insight...
Eric
It is not rewriting that is the problem. Your AJAX request is JavaScript and has nothing to do with the ASP.NET routing engine. When you use Class/AddClass you are making it relative to the location of the current URL. You can use /Class/AddClass which will resolve to the root of the site. That poses an issue if you are even in a virtual directory. I prefer to pull the full URL from a configuration file:
var url = '<%: ConfigurationManager.AppSettings["WebsiteURL"] %>/Class/AddClass';
With the appropriate entry in the web.config. This eliminates any guess work. You can also use ResolveUrl:
var url = '<%: ResolveUrl("/Class/AddClass") %>';

MVC Deployment Problem - Site Loads but Links (Routes) do not

Having a problem deploying an MVC application.
Basically the site loads correctly, the home page appears. However anything which needs to access a controller action does not. So all the links just throw up 404 errors.
Does anyone have an Idea why the site loads but after that the controller actions appear not to?
Thanks
Are you running your app in IIS 6? If so you'll need to configure the .mvc extension or configure wildcard mapping. Steve Sanderson has a good post on it.
The fact that the home page appears indicates that you have at least one controller working properly. Namely, the HomeController.
You should check that you are following the default conventions (if you have it set up that way)
Controllers belong in the Controllers folder and follow the naming convention [Name]Controller.
Also, every action in the controller must be public and must return an ActionResult of some kind. Returning a View will cause a particular View to be rendered.
Also, Views follow the folder structure View/[ControllerName]/[Action].aspx
The fact that the first page loads means you probably have Home/Index set up properly for both your Controller and your View. You should take a look at those and see what the difference between that is and the other controllers/actions/views that you've set up.

Resources