MVC - javascript file route with .js extension - asp.net-mvc

Scenerio is that I want to hide the physical paths of javascript files in my MVC web application & I tried to do so as following in my RouteConfig!
routes.MapPageRoute("js", "js/base.js","~/scripts/applicationbase.js");
But, the path produces 404 error!
And it worked when I removed the .js extension from the route like this:
routes.MapPageRoute("js", "js/base","~/scripts/applicationbase.js");
Does it means that a route with file extension can't be created? If not then how can I do that?

Related

Zendesk - How to include custom js file in client side app?

I am trying create Clint side in Zendesk framework.
I want to include custom js file to perform some action (to keep it separate).
directory structure
app-folder
assets
iframe.html
.
.
sevices
userServices.js
tmp
translations
en.json
manifest.json
I have included file like following (in iframe.html)
<script src="/base-app/services/userServices.js"></script>
i am getting following error
GET http://localhost:4567/base-app/services/userServices.js
net::ERR_ABORTED 404 (Not Found)
Move the 'services' folder under 'assets' and update the script tag to
<script src="services/userServices.js"></script>

Relative URLs within sub application on IIS site

I have "test" website on IIS and "sub1" sub-application within it.
When I request domain.com/sub1, IIS processes sub-application sucessfully.
Some of URLs in code are relative, and when I have something like this in code /component/test, application requests following url domain.com/component/test instead of domain.com/sub1/component/test.
What do I need to change in order to make relative URLs to be valid.
in your code, use #Url.Content("~/") or <% =Url.Content("~/") %> to get to the relative root of the application.
Or, sometimes, you can get away with prefixing with ~/, but generally only with js and css files in your layout
note: the # is for razor view engine, and the <% %> is for aspx view engine, you didn't specify which you were using
EDIT: (based on comment from OP)
CSS should be relative from the directory your CSS is hosted, you can use ../ to traverse directories. For your JS, can you edit those files? I would define a SITE_ROOT variable (above all js files) in your layout and then reference that variable from within your JS files
example: var SITE_ROOT = '#Url.Content("~/")';
then prefix your calls in the JS file with SITE_ROOT + '/relative_url'

How to write path redirect in ruby on rails and angularjs

Sorry that I am new to ruby on rails.
I am trying to create a directive in angularjs.
What I was doing is create an html file in folder view/forms, named topRight-buttonGroup.html
Then I just created a simple directive for test:
app.directive('topRightButtonTools', function(){
return {
restrict: 'C',
templateUrl: 'topRight-buttonGroup.html'
};
});
But getting the error message in the console is:
GET http://localhost:3000/forms/topRight-buttonGroup.html 404 (Not Found)
Am I missing something should be done on ruby on rails? Or should I write some redirect code and save in somewhere?
my full folder structure is:
If you need the form to be accessible so, create a folder inside public/ as forms/ and have the file placed there. Only content inside the public dir are rendered directly.
Unlike core php or other such scripting languages, RoR requires controllers to render actions which arent merely files that are interpreted. Also, routes are to be defined in config/routes.rb.
Refer:
http://guides.rubyonrails.org/routing.html
http://www.xyzpub.com/en/ruby-on-rails/3.2/statische_webseiten.html

deployment path issue in css

I have to rewrite a legacy asp.net mvc app using lots of:
Url.Content()
to get it to work in a different/additional deployment environment where the virtual directory is a sub directory of the default site.
At the moment css classes like this:
.icos-pencil:before { content: url(/content/images/global/icons/usual/icon-pencil.png); }
are also broke. Is there a similar 'helper' (?) like Url.Content for the css above?
If you want use helper in js or css file you can write own view engine such as jsHelper or you can use this code
background-image:url('../../content/images/global/icons/usual/icon-pencil.png');
becomes
background-image:url('content/images/global/icons/usual/icon-pencil.png');
Simply use relative path in your CSS.

Asp MVC 3: Dynamically resolve relative resources in views

I have a javascript application that runs in a view (index.cshtml).
Problem:
The problem is all relative paths are relative to the current url, which would be ok in a simple html webapp but not in asp mvc. The js-app shouldn't have to bother whether it's served in a normal html file or via a asp mvc page.
I.e. http://www.domain.com/<controller>/<action>/ contains a script test.js. This script loads an external xml file searching relative to it ie. "data/data.xml". The resulting url reads http://www.domain.com/<controller>/<action>/data/data.xml. This isn't found.
Question:
Is there a way to route static files (images,..., maybe even js files) to the content folder like "~/Content/controller/action/<pathToFile>/"?
Any help appreciated!
Lg
warappa
PS: I know about Url.Content() but that doesn't fit here.
The solution doesn't require mapping - just a simple html tag in the header:
<base href="#(Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped) +
Url.Content("~/content/controller/action/"))" />
Lg
warappa
EDIT
Some browsers need an absolute url - sample updated.
In you can use absolute URL addresses to access you static resources:
$('img').attr('src', '/Content/Pictures/picture1.png');
or
<script src="/Scripts/script.js"></script>
This way you will allways get the same resources relative to the page base address, no matter if you load the script in a /{Controller}/{Action}/{View}, {Area}/{Controller}/{Action}/{View}, a custom route or even in a static script html page.
Or perhaps what you're looking for is the use of css files, since CSS's url('<path>') resolves the addresses relative to the CSS file's location. You would just need to import the one CSS file that had all the resource (image?) file paths. Then the scripts could reference the distinct class names, thus not being location aware at all. This is what libraries like jQuery UI do. But again this would require a fixed folder structure relative to the CSS document.

Resources