I am converting an asp classic (.asp) to mvc4. The site is fine, but when I try to map the legacy pages to the new routes it fails big time.
I just want to : default.asp to / or pageA.asp to /NewPageA
When I try to map the old pages , looks like the asp classic handler intercepts first the request and try to find the page .asp, but as doesn't exist, it throws 404 error.
I am mapping wrong ? my mapping is like this:
routes.MapRoute("DefaultOld", "default.asp", new { controller = "Home", action = "IndexOld" });
routes.MapRoute("MenuOld", "menu.htm", new { controller = "Home", action = "IndexOld" });
routes.MapRoute("RecepcaoOld", "recepcao.htm", new { controller = "Home", action = "IndexOld" });
routes.MapRoute("EntradaPorOld", "entrada_por.htm", new { controller = "Home", action = "IndexOld" });
Is possible to tell the server to ignore the classic pages and let the mvc route to intercepts the request and act as it should ?
I am using IIS 7.
I tried this solution here (http://blog.abodit.com/2010/04/a-simple-redirect-route-handler-for-asp-net-3-5-routing/), works fine to map tp aspx pages, but when I try to use with asp or htm, it doesn't work at all. So I think I need t disable the handler od classic asp, is that correct ? but in the htm side, the StaticHandler if I disable it, the images and all static resources would not work well, correct ?
cheers
Related
I am using MVC3, C#, Razor, mvcSiteMapProvider V4.
I am using "Mvc.sitemap"
<mvcSiteMapNode title="Reports" controller="Report" action="Index" preservedRouteParameters="ClientId" route="Report">
<mvcSiteMapNode key="Report_Section" title="Sections" controller="Section" action="FilterByReport" preservedRouteParameters="ClientId,ReportId" route="Report_Section">
<mvcSiteMapNode key="Background" title="Background" controller="Wizard" action="Index" preservedRouteParameters="ClientId,ReportID,SectionID,SectionName" route="Background"/>
The "Global.asax" custom routes look like:
routes.MapRoute("Report", "Report/{ClientId}", new { controller = "Report", action = "Index", ClientId = UrlParameter.Optional });
routes.MapRoute("Report_Section", "Report/{ClientId}/Section/{ReportId}", new { controller = "Section", action = "FilterByReport", ReportId = UrlParameter.Optional });
routes.MapRoute("Background", "Report/{ReportID}/SectionID/{SectionID}/SectionName/{SectionName}", new { controller = "Wizard", action = "Index", ReportID = UrlParameter.Optional, SectionID = UrlParameter.Optional, SectionName = UrlParameter.Optional });
The "Report" and "Report_Section" routes work fine. However when I go into the "Background" route I lose all of my route structure, for the "Report_Section" and "Report" routes, in the mvcSiteMap BreadCrumb URL. Instead I get a GUID ie:
http://localhost/7ebe9bb9-a663-43fd-9fb1-865866be12b9
I believe this might be the XML Node Key that is autogenerated. However it prodocues a 404 when clicked.
I should get something like:
Reports
http://localhost/Report/10
Sections
http://localhost/Report/10/Section/100
Any ideas what could be causing this?
Thanks.
You will get a Guid for the URL if the URL resolver cannot find a match. Throwing an exception in this case was causing other issues (see why does URL resolver throw an exception).
However, I can't tell you exactly why it is not matching without seeing more of your configuration. A clue is that we are using UrlHelper.RouteUrl(string, RouteValueDictionary) to resolve the URL. You can try calling it explicitly with your route values and route name. Also, see the source code for other clues.
One thing I noticed that looks off is that you are preserving the ClientID route parameter for Background when it is not even used in that route. Keep in mind that preserving route parameters only copies them from the current HTTP request and your other nodes will appear to forget them.
PreserveRouteParameters is typically used for CRUD operations where you make data editing pages. If you want it to appear to "remember" the user's navigation path, then you need to add 1 node per unique route value combination to your sitemap.
If the above doesn't help, please create a small demo project that exhibits the problem and either upload it to GitHub or else zip it and make it available for download, then open a new issue at GitHub with the link to the demo.
I'm using Fancybox 1.3.4 with ASP.NET MVC 3.
I have following link :
<a id="various" href="Like/List/#feed.Id" class="petlikeCount liked">#feed.LikeCount</a>
and also jquery :
<script type="text/javascript">
$(document).ready(function () {
$("#various").fancybox({
type: 'ajax'
});
});
</script>
Controller action in Like controller :
public JsonResult List(int id)
{
return Json("success", JsonRequestBehavior.AllowGet);
}
My problem is that Like/List is never called (checked with the breakpoint) and fancybox just appears and show content of "parent" page....
I also tried with iframe content returning pure html back, but I'm getting same strange behavior as above.
Thank you in advance!
I'd recommend you using HTML helpers instead of hardcoding anchors:
#Html.ActionLink(
feed.LikeCount,
"List",
"Like",
new { id = feed.Id },
new { id = "various", #class = "petlikeCount liked" }
)
Another thing that you should make sure is that the feed.Id is actually an integer variable so that when the List action is invoked it is correctly passed this id.
So your url should look something like this: /List/Like/123. And then assuming tat you have kept the default route and haven't messed up with some custom routes, the List action should be called and passed the correct id as argument.
Also I would very strongly recommend you using a javascript debugging tool in your browser such as FireBug in which you will be able to see any potential errors with your scripts as well as the actual AJAX requests being sent which will allow you to more easily debug such problems.
My first post...
When I use RedirectToAction the url in the browser doesn't change. How can I achieve this?
I'm switching over to ASP.NET MVC 3.0 (also using jQuery Mobile) after 10+ years using web forms. I'm about 8 weeks into it, and after several books and scouring Google for an answer, I'm coming up dry.
I have a single route defined in Global.asax:
routes.MapRoute(
"Routes",
"{controller}/{action}/{id}",
new { controller = "Shopping", action = "Index", id = UrlParameter.Optional }
I have a ShoppingController with these actions:
public ActionResult Cart() {...}
public ActionResult Products(string externalId) {...}
[HttpPost]
public ActionResult Products(List<ProductModel> productModels)
{
// do stuff
return RedirectToAction("Cart");
}
The url when I do a get and post (with the post having the RedirectToAction) is always:
/Shopping/Products?ExternalId=GenAdmin
After the post and RedirectToAction I want the url in the browser to change to:
/Shopping/Cart
I've tried Redirect, and RedirectToRoute but get the same results.
Any help would be greatly appreciated.
[Update]
I found that jQuery Mobile AJAX posts are the culprit here. If I turn off jQuery Mobile's AJAX it works.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
// do not handle links via ajax by default
$(document).bind("mobileinit", function () { $.mobile.ajaxEnabled = false; });
</script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.css" />
The ordering of the above scripts is important. I had to include the script to jQuery first, then include the script to disable jQuery Mobile's use of AJAX and then include the script to jQuery Mobile.
I'd still like to find a way to use AJAX and have the url update properly. Or at the least be able to call jQuery Mobile's "loading" message (or bake my own).
I think I've found an answer. Buried deep in the jQuery Mobile documentation, there is information about setting the data-url on the div with data-role="page". When I do this, I get the nice jQuery Mobile AJAX stuff (page loading message, page transitions) AND I get the url in the browser updated correctly.
Essentially, this is how I'm doing it...
[HttpPost]
public ActionResult Products(...)
{
// ... add products to cart
TempData["DataUrl"] = "data-url=\"/Cart\"";
return RedirectToAction("Index", "Cart");
}
Then on my layout page I have this....
<div data-role="page" data-theme="c" #TempData["DataUrl"]>
On my HttpPost actions I now set the TempData["DataUrl"] so for those pages it gets set and is populated on the layout page. "Get" actions don't set the TempData["DataUrl"] so it doesn't get populated on the layout page in those situtations.
The only thing that doesn't quite work with this, is when you right-click... view source... in the browser, the html isn't always for the page you are on, which isn't unusual for AJAX.
Not sure if it is still actual, but in my case I wrote following helper method
public static IHtmlString GetPageUrl<TModel>(this HtmlHelper<TModel> htmlHelper, ViewContext viewContext)
{
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.Append("data-url='");
urlBuilder.Append(viewContext.HttpContext.Request.Url.GetComponents(UriComponents.PathAndQuery, UriFormat.UriEscaped));
urlBuilder.Append("'");
return htmlHelper.Raw(urlBuilder.ToString());
}
And then use it as follows:
<div data-role="page" data-theme="d" #Html.GetPageUrl(ViewContext) >
This way I don't need for every redirect action store a TempData. Worked for me fine both for Redirect and RedirectToAction. This would not work properly in case if you use method "View()" inside controller and return different view name, which will change UI, but will retain url.
Hope it helps
Artem
David, this was a big help to me. I just wanted to add that in my case I had to use the following format to get the Url to display in the correct form as my other url's:
TempData["DataUrl"] = "data-url=/appName/controller/action";
return RedirectToAction("action", "controller");
As a side note, I also found that when assigning the value to TempData["DataUrl"] I was able to leave out the escaped quotes and enter it exactly as above and it seems to be working fine for me. Thanks again for your help.
There is an issue on github
https://github.com/jquery/jquery-mobile/issues/1571
It has a nice solution without the need of TempData
This is for an ASP.NET MVC3 web application. In RegisterRoutes I have the following as my only line:
routes.MapRoute("Default", "Configuration", new { controller = "DeviceConfiguration", action = "Index" });
When I run the project, going to the URL /Configuration/ gives me a 404 error. However if I change the word Configuration to any other word, such as:
routes.MapRoute("Default", "Configuratio", new { controller = "DeviceConfiguration", action = "Index" });
Then going to the URL /Configuratio/ loads just fine. It seems as if ASP.NET is simply refusing to route to the URL /Configuration/.
Again, this is the only line in RegisterRoutes; I've tried commenting out everything else to debug this. I have no MapRoute or IgnoreRoute calls in my code anywhere else, and I am not editing the routing table in any location.
How can I change this behavior?
I suspect that you have a physical folder called Configuration under the root of your application. The ASP.NET MVC routing engine has preference for physical folders over routes. One possible way is to set the RouteExistingFiles property to true after your route definition:
routes.MapRoute(
"Default",
"Configuration",
new { controller = "DeviceConfiguration", action = "Index"
});
routes.RouteExistingFiles = true;
I'm using jquerymobile and asp.net MVC3 razor. I'm somewhat new to this, but figuring things out, I have a very basic route table, with just the ignore route for .axd resources and a default route:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "", UrlParameter.Optional }
);
I have an action link that works in an .ascx page:
#Html.ActionLink("Log On", "LogOn", "Account", null, new { #class = "ui-btn-right" })
Now, when I get to the LogOn page, and I complete the account verification, I need to send them to the home page, /Home/Home which works if I type it in the address bar, but I can't seem to get my redirect working in the account controller: return RedirectToAction("Home", "Home");
Thanks for any help!
It sounds like maybe you're posting your form from /Account/LogOn via ajax. That means that when you return RedirectToAction("Home", "Home");, the page wouldn't change. So you just need to not use ajax on the form submit. If my guess is wrong, please post more code so we can get an idea of what the problem is.
It appears from the documentation that jQuery mobile handles form posts via ajax automatically. To prevent this, add the data-ajax="false" attribute to the form element. Documentation here: http://jquerymobile.com/test/docs/forms/forms-sample.html
Given what's written in documentation, could you put your code into a form element and setting its action property to /Account/LogOn ? In order to do that, you may have to convert your ActionLink into a button.
You can check for ajax request in controller:
if(Request.IsAjaxRequest())
{
//send json object and do navigate in client
return Json(new {IsLogged = true});
}
else
{
//send redirect html code to browser
return RedirectToAction("Home", "Home");
}
and in client side, check for json result if you post form by jQuery ajax.
I'm not use jquerymobile so I don't know how it work in client side.