T4MVC causing incorrect URL's - asp.net-mvc-3

Using T4MVC on my new MVC Razor Project and I will have an action link like so
#Html.ActionLink(ViewRes.SharedStrings.HomeLink, MVC.Home.Views.Index, null, new { rel = "dropmenu7" })
so i would expect a url like
http://localhost:52122/Home/Index
but what i am getting is
http://localhost:52122/Home/~/Views/Home/Index.cshtml
looking into the t4mvc template file, i see where the "~/Views/Home/Index.cshtml" is coming from but I dont wanna touch it because it was made that way and i would guess i shouldnt have to change anything there.
Asking a friend, he says that i should use RouteLink instead of ActionLink because i am sometimes going to locations outside of the controller. However when i do that, i get: "A route named '~/Views/Home/Index.cshtml' could not be found in the route collection." when i try to run the app.
I guess i should also note that the links i am using are in the _Layout.cshtml
What am i doing wrong?

You need to change 'MVC.Home.Views.Index' to 'MVC.Home.Index()':
#Html.ActionLink(ViewRes.SharedStrings.HomeLink, MVC.Home.Index(), null, new { rel = "dropmenu7" })

Related

Codeigniter subfolder named like controller

I'm trying to make folder in controller folder with exact name like controller in Codeigniter. Is it possible by some trick or something?
Screenshot here: http://i.imgur.com/vrQ1J9V.png
If it will be like
/controllers/manage/manage.php
you should add in /config/routes.php
$route['manage/(.*)'] = "manage/manage/$1";
$route['manage'] = "manage/manage";
There is no problem in using the same name, but it is confusing. The best solution would be to change the name, but you can use just fine.
Remember to use the routes with 'manage/manage/function'.
EDIT
I incorrectly viewed the first time and thought manage.php was outside the manage folder.
It will work, but your URL will just have "manage" twice. You could change the routes config to remove the first "manage", but it will be less confusing and time-consuming to just name manage.php something different.

Organize views and controllers with the new EmberJS router

I want to organize my views in packages / folders to avoid a long list. With the (great) new router, a view is provided by default that we can change by creating a new one with a convention name. For example:
match('/').to('home');
uses:
"home" handlebars template
App.HomeView
App.HomeController
Now I want to use:
"my_package/home" handlebars template (works)
App.MyPackage.HomeView
App.MyPackage.HomeController
When I use the gem "ember-rails" (the GIT version) and the generator:
rails g ember:view my_package/home
I get:
DemoEmberRails.MyPackage::HomeView = Ember.View.extend({
});
that is not a correct javascript code (seems to be an extract for ruby code).
I tried:
DemoEmberRails.MyPackage = {};
DemoEmberRails.MyPackage.HomeView = Ember.View.extend({
});
But it's not used by the router.
How to do that?
I think you should Namespace them using Ember.Namespace. I'm still not sure if the router will automatically search namespaces but it may?
http://emberjs.com/api/classes/Ember.Namespace.html
As you've said
match('/').to('home');
expects AppName.HomeRoute, AppName.HomeView and AppName.HomeController. So if you have a template with data-template-name="home" and a view similar to
AppName.HomeView = Ember.View.extend({
teplateName: 'home'
});
then ember will automatically connect the / route with this view.
The new ember routing guides are quite helpful here.
It looks like this is currently unsupported, however, a pull request exists to add this feature.
See:
https://github.com/emberjs/ember.js/pull/1679

Load single post using Ajax for WPML based site

I want to load single posts into an index page using Ajax for a WPML based site that I'm working on. Not sure what I'm missing but there seems to be a catch when using multiple languages and fetching the correct posts.
Here's the code I wrote -
function loadProject(reference) {
console.log(window.location.origin + '/references/' + reference);
// Displays "http://mysite.com/references/example-post/", which exists
$('#content').load(window.location.origin + '/references/' + reference, function() {
console.log('Load was performed.');
});
}
I don't reach the JS log after load() but I don't receive any error message either (e.g. "File not found").
"References" is a custom post type that I've created using the Types plugin, and has a file of it's own called single-references.php. I'm not entirely sure how CPTs work with WPML, and that's probably where the problem lies. I have two languages, Swedish and English, and the CPT only has one slug for its kind ("references").
[Solved]
Used the .get() method instead to retrieve the post and now it works.
Well, no console error, no network query :
it is not a WPML issue
did you check #content really exists ?
In chrome dev tool, go to Network, and trigger your function. You'll see what you receive.

Routing document-relative static urls in MVC3

I'm integrating a JavaScript library into an ASP.NET MVC3 web app. The library assumes it will be installed next to the page that references it, and so it uses document-relative URLs to find its components.
For example, the default directory layout looks like
container-page.html
jslibrary/
library.js
images/
icon.png
extensions/
extension.js
extension-icon.png
However, I want to reference the library from the view in /Home/edit. I install the library in the default Scripts\jslibrary\ When I reference the library in the view in Views\Home\edit.cshtml, the library's document-relative links like
images/icon.png
end up as requests to
http://localhost/Home/images/icon.png
which results in a File Not Found (404) error. How do I construct a route to look for
{anyControllerName}/images/{anyRemainingPathInfo}
and serve up
http://localhost/Scripts/jslibrary/images/{anyRemainingPathInfo}
?
(full disclosure: I'm still on IIS 6 in Production, and not much chance of going to IIS7 any time soon, so if this is better done at the IIS level, please account for IIS6. Thanks!)
You could create a controller for handling you redirect logic - for example an "Images"controller. Register a global route in your Global.asax file, using the pattern (more on this type of pattern here:
routes.MapRoute(
"Images", // Route name
"{xyz}/{controller}/{path}", // URL with parameters
new {controller = "Images", action = "Index", path= UrlParameter.Optional} // Parameter defaults);
In your controller:
public ActionResult Index(string path)
{
//format path, parse request segments, or do other work needed to Id file to return...
return base.File(path, "image/jpeg"); //once you have the path pointing to the right place...
}
Not sure if this solution will work for you, wish I could come up with something more elegant. Best of Luck!
Short of rewriting the library and having it check for the appropriate directory the only solution I can think of is to include the views, library and supporting files in a directory structure that the library can access. This of course would break MVC's convention over configuration way of finding views, so you would have to write a custom override of the way Razor looks for views, which is not too complex to do, but you might be making life more difficult for yourself down the road depending on your application. Your call which is the lesser of the two evils :) (I'd go for fixing the library)
Make a help function
#functions{
public string AbsoluteUrl(string relativeContentPath)
{
Uri contextUri = HttpContext.Current.Request.Url;
var baseUri = string.Format("{0}://{1}{2}", contextUri.Scheme,
contextUri.Host, contextUri.Port == 80 ? string.Empty : ":" + contextUri.Port);
return string.Format("{0}{1}", baseUri, VirtualPathUtility.ToAbsolute(relativeContentPath));
}
}
Calling
#AbsoluteUrl("~/Images/myImage.jpg") <!-- gives the full path like: http://localhost:54334/Images/myImage.jpg -->
This example are from
https://dejanvasic.wordpress.com/2013/03/26/generating-full-content-url-in-mvc/

How to use Html.Partial() method to render partial view with an explicit path

Everyone, I'm using MVC 3 (Razor). I have the following problem:
I have some common contents segregated into a partial view. But rather than to put it in the default location (views/shared or views/controller-name), I need to put it in a different location (views/shared/new-folder or view/controller-name/new-folder).
I tried this : #Html.Partial("views/shared/new-folder/partial-view-name") or even #Html.Partial("views/shared/new-folder/partial-view-name.cshtml"),but it seems that MVC3 only consider the parameter as a view name, and it totally ignored any path information.
Maybe I did something wrong ,can anybody help me with this ?:) Thank you very much!
You need to reference using an application virtual path (notice the ~\ at the beginning of the path):
#Html.Partial("~\\views\\shared\\new-folder\\partial-view-name.cshtml")
If you have also configured an action to return that Partial View, You could also do:
#{ Html.RenderAction("PartialViewAction", "PartialViewCOntroller");}
This is probably better since you shouldn't be hard coding references to Views in your code. Deploying a hard coded reference on a different server could break the application, but calling an action to return a view won't.

Resources