ASP.NET MVC urls for subsites - asp.net-mvc

In my Views I have urls mapped like /My/Urls.something, for example in links (when I don't use View helpers), including static content like images, and mostly in javascript code (Ajax calls and the like).
This works fine when I deploy my app in a first level domain, like http://mysite.com/ because the / before the Url remaps it to the root of the domain, but if the site is deployed as a subsite, for example http://mysite.com/myapp/, this doesn't worka anymore, and omitting the / (for example /My/Urls.something -> My/Urls.something) doesn't work in inner pages like http://mysite.com/myapp/admin where I want to read http://mysite.com/myapp/My/Urls.something but I get http://mysite.com/myapp/admin/My/Urls.something.
Any help?

You should always use view helpers. Even if you don't want to use the ActionLink helper, you should use the Url.Action helper to generate your url. Why? Because of the very problem you're trying to solve, which the helpers were designed to fix.
For example:
<a href="#Url.Action("Index", "Home")" />

Related

How to use native anchor links with angularjs

I'm using angularjs on a rather large flat documentation page. The page has some navigation thats designed to use traditional url hash links. The urls look like so:
/documentation/flat#26166276-basic-events
These urls get rewritten once the navigation occurs and i've hit the next page. angular initializes to something like:
/documentation/flat#/26166276-basic-events
This breaks the navigation. It seems to work if I am already on the /documentation/flat path and hit one of the hash urls. It gets rewritten but the browser still focus's on the correct section of the page.
However if the the hash url is triggered from a different path the browser will not focus on the correct DOM element as the angularjs rewrite happens.
Edit: this is what the markup for a link looks like
Basic Events
<h1 class="chap-header" id="26166276-basic-events">2.1.0 Basic Events</h1>
This topic was further discussed here:
How to handle anchor hash linking in AngularJS
I used a variation from that thread
if $location.$$url[0]== '#'
$location.hash($location.$$url.replace('#', ''))
$anchorScroll()
that basically lets me prefix any anchor links with an additional # and angularjs treats them as traditional anchor
There is a very silly solution: put a / at the start of the anchor id!
<a id='/my-id' />

Url.Content() not working on complex URLs

I have Url.Content() snippets everywhere in my views. They work fine on pages with a simple URL, but not when the URL gets longer.
Here's a sample:
<img src="#Url.Content("~/Content/Images/logo.png")" id="logo">
This works fine when I'm on the homepage, or a page with URL localhost:8080/s/chocolate (which shows the result for the "chocolate" search.
But when I'm trying to add some refinements, e.g. localhost:8080/s/chocolate/b/lindt (which means filter the previous results to only ones from brand Lindt), it doesn't work anymore. In this case, Url.Content points to /s/chocolate/Content/Images/logo.png, which obviously fails.
It's as if Url.Content only goes 2 levels up the current location instead of using the real root of the web app. I guess it makes sense in the convention that URLs are in the form host/controller/action, but here I have a more complex URL scheme (I use URL rewriter module to match these URL fragments to action's parameters).
Is there any way to tell the helper to go to the real root, or any other solution to this problem?
(BTW, I'm using MVC 4)
EDIT:
As Felipe answered, I've just discovered that Url.Content is no longer necessary with MVC 4. That works for all "design" images with a constant path. However, I use a lot of images where the path is constructed partly with some data, e.g.
<img src="#Url.Content(string.Format("~/Content/Images/stores/{0}.png", cart.Store.Retailer.Id))"/>
I simply removed the Url.content, as such:
<img src="#string.Format("~/Content/Images/stores/{0}.png", Model.PreferedCart.Store.Retailer.Id)"/>
When rendered, this gives the following src: ~/Content/Images/stores_v2/Fr_SimplyMarket.png. The ~ being still here, the image is not found. How can I fix that?
In ASP.NET MVC 4+, you do not need to use Url.Content in attribute values. Use the ~/ syntax directly and let razor view engine 2 process the path for you. So your examples can be coded as follows:
<img src="~/Content/Images/logo.png" id="logo">
In the case you need to create a dynamic path, so, you have to use Url.Content, for sample:
#{
string imagePath = Url.Content("~/Content/Images/stores/" + Model.PreferedCart.Store.Retailer.Id + ".png");
}
<img src="#imagePath"/>
If it does not work, the reason is because you have problems with your URL rewriter.
In ASP.NET Core 5, It's working fine using below code in razor page.
#{
string logoPath = Url.Content("~/img.jpg");
}
<div style="background-image:url(#logoPath);">

html.beginform v old skool form tag

What is the benefit to using Html.BeginForm?
We're having some issues related to html helpers and razor generator view testing and I'm struggling to see the benefit which would stop us going back to old skool form tags.
Has anyone got an argument for or against either?
by old skool i mean:
<form action="#Url.Action('Blah')">
The Html.BeginForm is useful because it generates the url using the routes defined in the Global.asax. (or you can extend it with your own code)
Using the old tag is neither worst or best in my opinion. You simply have to generate your url manually or using the Url helper. In the end the html in the page will be the same
<form ....>
html
</form>
Html.BeginForm also implements IDisposable, meaning the form must be closed properly. It's a minor thing, perhaps, but not closing Html.BeginForm produces a run-time error, where an unclosed <form> tag does not.
no there is no difference , the form tag just use the routing to generate the url , so if you use #Url.Action you are good to go
there is even books use that way a plain old tag and a url helper to generate the route
ASP.NET MVC Website Programming is an example
Edit
**
starting from Mvc 4 there is no difference , prior to Mvc 4 , Mvc 3 for example require the Html.BeginForm to make the javascript unobtrusive validation to work

Using MVC Routes as Shortcodes

We have been trying to implement shortcodes on an ASP.NET MVC web app that allow users to uniquely invoke a given article/page using an assigned short code.
For e.g.: www.mysite.com/power would map to an actual URL: www.mysite.com/Power/Home/.
I have created various routes throughout the site that map these shortcodes to various actions and controllers within the application. From a shortcode/route point of view, everything is working great.
I, however, noticed a couple of interesting things. I have hyperlinks that I use Url.Action method to generate the URL pointing pages. Many of these pages also have short codes associated with them. For e.g.: I have a link that says:
Go to Power page
This is a page that also has the previously mentioned short-code assigned to it. When I use Url.Action, I ideally expect it to create a link as /Power/Home/Index or /Power/Home, but since I also have a route constraint mapped to it, it now generates the link as /power.
Is there a way I can just use the actual link URL when generating links? I only want short-codes when I am sending out emails etc. I want the site to generate actual URLs.
This may or may not be possible, but I wanted to see if there were any ideas out there that I could use.
Anup
Index and Home are likely defined in your route table as defaults for the Action and Controller element. When you generate the Url it wont include the defaults if they aren't needed.
You could write your own Action overload or helper, which would allow you to take more direct control of the generated URL or action link. You could approach it from two different ways: 1) a helper to generate short-code specific urls and links, and/or 2) a helper to generate the full url and/or link. If Url.Action is returning the short-code version due to your routing configuration, I'd think a good place to start would be the second option, creating a helper/extension method that will generate the full url for you.
Here's how I solved this:
Instead of naming a route with short code to point to the action url, I made the route point to a different Controller action which would then redirect to the actual route that I want it to.
For e.g.: Originally I had the code "power" defined in the route table such that it would point to www.mysite.com/Power/Home.
Now instead of pointing it to that action - Index, controller - Home, area - Power, I make it resolve to: action - Power, Controller - Home, Area - ShortCode.
In the controller now, I simply do a RedirectToAction("Index", "Home", new { Area = "Power" });
This ensures that the actual links to /Power/Home do not resolve to the shortcode "power".
This is a simple fix increased the work by a little bit, but works like a charm.

Using JQuery with ASP.NET MVC Framework

I have searched the forum, and google for this topic. Most of the articles are talking about using JSON to call the controller/action on the server and do ajax effect on the result.
I am trying to use some very basic JQuery features, like the JQuery UI/Tabs, and JQuery UI/Block for a dialog window. I cannot get these simple samples to work in my MVC project. Any ideas how I should modify these samples? I only need these basic feature now and I can go from here.
Thanks!
Actually I just got it working. The problem is that I need to modify the path to an absolute path to the view page because the relative path doesn't work with the MVC routes {controller}/{action}/{id}.
Thanks!
For info, re the relative path issue - I discussed this here (the same concept applies to any page, not just master pages). The approach I used is like so:
1: declare an extension method for adding scripts:
public static string Script(this HtmlHelper html, string path)
{
var filePath = VirtualPathUtility.ToAbsolute(path);
return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>";
}
2: when needed (for example in the <head>...</head>) use this method:
<%=Html.Script("~/Scripts/jquery-1.2.6.js")%>
The advantage of this is that it will work even if the web app is hosted in a virtual directory (i.e. you can't use "/Scripts" because you aren't necessarily at the site root) - yet it is a lot clearer (and less messy) than the full script with munged src, i.e.
<script ... src="<%=Url.Foo(...)%>"></script>
I just implemented the jquery autocomplete textbox in one of my asp.net project. I only had to import the js file and drop some code into my aspx page. Could you be more detailled about what sample you are trying to run?
This is quick response!!
I am trying to run this "Simple Tabs" on this page:
http://stilbuero.de/jquery/tabs/
I think it is the same with this one: http://docs.jquery.com/UI/Tabs
I just copied and pasted the whole thing into my MVC view page, with corrected path to the jquery.js and .css files, but the content in the tabs all show up together (two of them are supposed to be hidden). My understanding is that this simple jquery plugin just show and hide content.
I had the exact same problem with the jquery thickbox plugin, that the item marked as "hidden" (the dialog box) will always show up in my MVC view page.
I can understand some of the MVC+Jquery+json articles, but I don't understand why the hide/show doesn't work.
Thanks!
I just made a walkthrough on how to do this:
http://blogs.msdn.com/joecar/archive/2009/01/08/autocomplete-with-asp-net-mvc-and-jquery.aspx

Resources